echo表的问题

时间:2012-08-06 11:53:10

标签: php

对于大多数人来说,这可能是一个非常简单的问题,但我无法自己找到解决方案。我刚开始使用基本的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宽度,如果有更多图片,则新行等。 / p>

非常感谢任何帮助!

亲切的问候

问题解决了(非常感谢你的帮助):

<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>

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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); 
}