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

私の問題は、すべてのサムネイルが互いに隣り合っているのではなく、互いの下にエコーされていることです.幅219の4つの列が互いに隣り合っており、さらに写真がある場合は新しい行など.

どんな助けでも大歓迎です!

敬具

問題が解決しました (ご協力いただきありがとうございます):

<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

これを試して...

$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 に答える