【问题标题】:Repeating PHP Image and Name in Div/Table Rows在 Div/Table 行中重复 PHP 图像和名称
【发布时间】:2013-08-07 22:31:39
【问题描述】:

此脚本查看目录,识别所有 gif、jpg、jpeg 和 png 文件,并将它们显示在正文部分。该目录有图像,然后是每个图像的副本,末尾添加了“_t”。显示的“_t”图像是指向全尺寸图像的链接,这些图像显示在灯箱中。它本质上是一个缩略图系统。

图片下方是删除了目录/扩展名的文件名,存储为$filename。

它改编自 Lightbox2 的显示脚本,以 hack 的形式编写,以使用自动生成的缩略图显示内容。我的问题是关于在 PHP 中格式化回显缩略图。

这是我目前的代码:

<?php
    function lightbox_display($dir_to_search, $rel){

        $image_dir = $dir_to_search;
        $dir_to_search = scandir($dir_to_search);
        $image_exts = array('gif', 'jpg', 'jpeg', 'png');
        $excluded_filename = '_t';
            foreach ($dir_to_search as $image_file){
            $dot = strrpos($image_file, '.');
            $filename = substr($image_file, 0, $dot);
            $filetype = substr($image_file, $dot+1);
            $thumbnail_file = strrpos($filename, $excluded_filename);
                if ((!$thumbnail_file) and array_search($filetype, $image_exts) !== false){

echo "<table border=1><td><a href='".$image_dir.$image_file."' title='$filename' rel='".$rel."' data-lightbox=".$filename.">
<img src='".$image_dir.$filename."_t.".$filetype."' alt='".$filename."' width='100' height='80' title=''/ ><br>
$filename</td>
</a>

" ."\n ";
            }
        }
    }
    ?>

之前的结尾部分是我将文件名作为每个缩略图下方的“标题”拉入的位置。但是,该脚本会创建一个直列图像(以及每个图像下方的文件名),因为它会为每个图像创建换行符。

我想要做的是让表格单元格(或 div 等)彼此相邻出现,例如:

[ ][ ][ ][ ][ ][ ]

..然后在 5 个缩略图处开始新的一行。

如何在 foreach 期间格式化 echo 中的内容,这会导致每行回显 5 个图像,$filename 显示在每个图像,然后在它下面开始一个新行?

编辑:代码最初来自http://www.fatbellyman.com/webstuff/lightbox_gallery/index.php

【问题讨论】:

    标签: php image formatting echo lightbox2


    【解决方案1】:

    我在这个循环中使用了一些东西来生成具有特定行数的表(这里是一个测试):

    <?php
    $table_rows=5;
    
    $table = "<table border=1><tbody>";
    $i = 1;
    
    for ($j = 0; $j < 13; $j++){
    
        if($i == 1) {
            $table .= "<tr>";
        }
    
        $table .= "<td>".$j." ".$i."</td>";
    
        $i = (($i+1)%$table_rows);
    
        if($i == 1) {
            $table .= "</tr>";
        }
    }
    
    if($i != 1){
        $table .= "</tr>";
    }
    
    $table .= "</tbody></table>";
    
    echo $table;
    ?>
    

    我确信这不是最简单的方法,而且代码看起来很糟糕,但它通常对我有用。

    【讨论】:

    • 我会在 "$table .= ""; } $table .= "".$j." 之间包装我的 echo 函数。 ".$i.""; -- ?
    • 和 之间的文本替换为您要回显的文本。
    • 这不起作用。这只是在我的页面中间插入一个图像大小的表格,里面有 13 个较小的空白表格。
    • 我如何修复它见上文。
    【解决方案2】:

    这是我为实现这一目标所做的:

    <?php 
        function lightbox_display($dir_to_search, $rel){
    
            $image_dir = $dir_to_search;
            $dir_to_search = scandir($dir_to_search);
            $image_exts = array('gif', 'jpg', 'jpeg', 'png');
            $excluded_filename = '_t';
            $piccounter = 0; /* this is how we monitor the length of the rows */
                foreach ($dir_to_search as $image_file){
                $dot = strrpos($image_file, '.');
                $filename = substr($image_file, 0, $dot);
                $filetype = substr($image_file, $dot+1);
                $thumbnail_file = strrpos($filename, $excluded_filename);
                    if ((!$thumbnail_file) and array_search($filetype, $image_exts) !== false){
    /*Variable $piccounter is the number of thumbnails across in the page per row*/
        if ($piccounter >= 6){ echo "<tr />"; $piccounter=0; } /*Every 6, drop in a </tr> and reset the variable for the new row */
    echo "<td><a href='".$image_dir.$image_file."' title='$filename' rel='".$rel."' data-lightbox=".$filename.">
    <img src='".$image_dir.$filename."_t.".$filetype."' alt='".$filename."' width='100' height='80' title=''/ ><br> 
    $filename<br/> 
    </a></td>"; /*$filename is both the image name AND the caption in expanded lightbox*/
    $piccounter++; /*After each image, add 1 to the $piccount counter to keep track of the length of the rows*/
    
    
                    }
            }
        }
    
        ?>
    

    创建了变量 $piccounter 来跟踪打印了多少缩略图。当它达到 6(或任何数字)时,它会插入一个表格行。否则,foreach 进程会创建 TD。

    在页面本身上,我在此脚本调用周围添加表打开/关闭。

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签