回声表问题
对于大多数人来说,这可能是一个非常简单的问题,但我无法单独找到解决方案。我刚刚开始使用基本的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>
在您的代码提供了创造为每个图像的新行,使他们在对方出现。你需要做的是如下:
<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>
嗨,首先,非常感谢您的快速回复,由于一些奇怪的原因,这没有奏效,它只显示了2列而不是4(我的结果与脚本在帖子下方的友好绅士的结果完全相同)问我的一个朋友他是否可以看看,现在看起来似乎在工作,我用他给我的代码更新了主帖。再一次,非常感谢! - – 2012-08-06 14:02:56
尝试......
$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);
}
嗨,首先非常感谢您的快速回复,由于一些奇怪的原因,这没有奏效,它只显示了2列而不是4(我的结果与脚本一样,你的帖子下方的友好绅士发布的结果完全相同), ive问我的一个朋友他是否可以看看,现在看起来似乎在工作,我用他给我的代码更新了主帖。再一次,非常感谢! – 2012-08-06 14:00:26
因为你是“呼应”每个表行一个大拇指,你有这个问题。尝试在表格行中添加更多单元格。 – Andreas 2012-08-06 11:56:24