2012-08-06 45 views
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> 
+0

因爲你是「呼應」每個錶行一個大拇指,你有這個問題。嘗試在表格行中添加更多單元格。 – Andreas 2012-08-06 11:56:24

回答

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

嗨,首先,非常感謝您的快速回復,由於一些奇怪的原因,這沒有奏效,它只顯示了2列而不是4(我的結果與腳本在帖子下方的友好紳士的結果完全相同)問我的一個朋友他是否可以看看,現在看起來似乎在工作,我用他給我的代碼更新了主帖。再一次,非常感謝! - – 2012-08-06 14:02:56

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); 
} 
+0

嗨,首先非常感謝您的快速回復,由於一些奇怪的原因,這沒有奏效,它只顯示了2列而不是4(我的結果與腳本一樣,你的帖子下方的友好紳士發佈的結果完全相同), ive問我的一個朋友他是否可以看看,現在看起來似乎在工作,我用他給我的代碼更新了主帖。再一次,非常感謝! – 2012-08-06 14:00:26