0

对于大多数人来说,这可能是一个非常简单的问题,但我自己无法找到解决方案。我刚开始使用基本的 php 并尝试创建一个非常小的画廊脚本,它请求画廊目录中的所有图像并将它们回显到一个表中,该表应该有 4 列并且宽度为 876,我有以下内容:

<body>
<table border="0" cellpadding="0" cellspacing="0" id="gallery" width="876">
    <tbody><?php
$dir = "./gallery/"; 
$exten = 'jpg';
if ($handle = @opendir($dir)) 
{
    while (false !== ($file = @readdir($handle))) { 
        $bestand = $dir ."/". $file ;
        $ext = pathinfo($bestand);
        if($ext['extension'] == $exten)
        {   

            echo "<tr><td><a href='/gallery/timthumb.php?src=". $file ."&h=300&'><img src='/gallery/timthumb.php?src=". $file ."&h=134&w=190' target='_blank'></a></td></tr>" ;
        }
    }
    @closedir($handle); 
} 
?>
</tbody></table>
</body>

我的CSS看起来像这样:

#gallery { border-collapse: separate; empty-cells: hide;
        border: 0px; background-color: #FFF; }
#gallery td    { border: 0px;   width: 190px;
    height: 163px;  padding-left: 12px; float: left;
    padding-top: 10px; vertical-align: top;
    color: #000000; background-image: url(../images/bg.png); background-repeat: no-repeat; }

我的问题是所有缩略图都在彼此下方而不是彼此相邻,我想要 4 列 219 宽度彼此相邻,如果有更多图片,则新行等。

任何帮助将非常感激!

亲切的问候

问题已解决(非常感谢您的帮助):

<body>
<table border="1" cellpadding="0" cellspacing="0" id="gallery" width="876">
 <?php
  $dir = "./gallery/"; 
  $exten = 'jpg';
  $i = 0;
  $files = array();

  if($handle = @opendir($dir)) 
  {
   while (false !== ($file = @readdir($handle)))
   { 
    $bestand = $dir ."/". $file ;
    $ext = pathinfo($bestand);

    if($ext['extension'] == $exten)
    {   
     $files[] = $file;
    }
   }

   @closedir($handle); 
  }

  $total = count($files);
  $imgPerRow = 4;
  $counter = 0;
  $i = 0;

  if($total > 0)
  {
   foreach($files as $file)
   {
    $i++;
    $counter++;
    if($counter == 1)
    {
     echo '<tr>';
    }

    echo "<td><a href='/gallery/timthumb.php?src=". $file ."&h=300&'><img src='/gallery/timthumb.php?src=". $file ."&h=134&w=190' target='_blank'></a></td>" ;

    if($counter == $imgPerRow)
    {
     $counter = 0;
     echo '</tr>';
    }
    elseif($i == $total)
    {
     for($j = ($counter - $imgPerRow); $j < 0; $j++)
     {
      echo '<td></td>';
     }

     echo '</tr>';
    }
   }
  }
  else
  {
   echo '<tr><td></td></tr>';
  }
 ?>
</table>
</body>
4

2 回答 2

1

在您提供的代码中,您为每个图像创建了新行,因此它们将显示在彼此下方。您需要做的如下:

<body>
<table border="0" cellpadding="0" cellspacing="0" id="gallery" width="876">
    <tbody><tr><?php
$dir = "./gallery/"; 
$exten = 'jpg';
$i =0;
if ($handle = @opendir($dir)) 
{
    while (false !== ($file = @readdir($handle))) { 
        $bestand = $dir ."/". $file ;
        $ext = pathinfo($bestand);
        if($ext['extension'] == $exten)
        {   

            echo "<td><a href='/gallery/timthumb.php?src=". $file ."&h=300&'><img src='/gallery/timthumb.php?src=". $file ."&h=134&w=190' target='_blank'></a></td>" ;
        }

      $i++;
      if($i==4){
         echo '</tr><tr>';
         $i=0;
      }

    }
    @closedir($handle); 
} 
?>
</tr>
</tbody></table>
</body>
于 2012-08-06T11:58:51.587 回答
1

尝试这个...

$countTr = 0;
if ($handle = @opendir($dir)) 
{
    while (false !== ($file = @readdir($handle))) { 
        if($countTr % 4 == 0)
        echo "<tr>";

        $bestand = $dir ."/". $file ;
        $ext = pathinfo($bestand);
        if($ext['extension'] == $exten)
        {   

            echo "<td><a href='/gallery/timthumb.php?src=". $file ."&h=300&'><img src='/gallery/timthumb.php?src=". $file ."&h=134&w=190' target='_blank'></a></td>" ;
        }

        if($countTr % 4 == 3)
        echo "</tr>";

        $countTr++;

    }
    if($countTr % 4 != 0)
    echo "</tr>";

    @closedir($handle); 
} 
于 2012-08-06T11:59:42.227 回答