對於大多數人來說,這可能是一個非常簡單的問題,但我無法單獨找到解決方案。我剛剛開始使用基本的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>
因爲你是「呼應」每個錶行一個大拇指,你有這個問題。嘗試在表格行中添加更多單元格。 – Andreas 2012-08-06 11:56:24