2017-08-05 42 views
1

我想用PHP創建一個圖庫。我想從一個文件夾中獲取所有圖像,然後將它們顯示爲3行。我有它的工作,但前2個圖像搞砸了。顯示圖片庫的PHP問題

這是我已經試過:

$images = glob("$_SERVER[DOCUMENT_ROOT]/gallery/img*.{png,jpg,gif}", GLOB_BRACE); 
echo '<table width="100%>'; 
$count="-1"; 
foreach($images as $image) { 
    if ($count%3 == 1) { 
     echo '<tr>'; 
    } 
    $url=str_replace("/home/#####/public_html/gallery", "", $image); 
    echo '<td width="33%"><div class="gallery">'; 
    echo '<img onclick="window.location='.$url.'" src="'.$url.'" alt="Image Alt" width="400" height="300">'; 
    echo '</div></td>'; 
    if ($count%3 == 3) { 
     echo '</tr>'; 
    } 
    //echo $count; 
    $count++; 
    //echo "|".$count; 
} 
if ($count%3 != 1) { 
    echo ',</tr>'; 
} 
echo '</table>'; 

//echo print_r($images); 

這個工程樣的,但它使這樣的:

(這些只是照片,真實的照片都有點反感.. )

我知道我做錯了什麼,但我不知道是什麼!

+1

你可以分享生成的實際HTML。使它更容易看到發生了什麼問題。 – Cagy79

+0

這些照片都是..我無法準確檢查手機上的元素。 –

回答

1

有在你的代碼的一些錯誤(見註釋)。也許試試這個:

$images = glob("$_SERVER[DOCUMENT_ROOT]/gallery/img/*.{png,jpg,gif}", GLOB_BRACE); 
echo '<table style="width:100%">'; // error was here (missing ") 
$count = 0; // error was here (counter = "-1") 
foreach ($images as $image) { 
    // start <tr> on 0 
    if ($count == 0) { 
    echo '<tr>'; 
    } 
    $url=str_replace("/home/#####/public_html/gallery/", "", $image); 
    echo '<td style="width:33%"><div class="gallery">'; // alternative 
    echo '<img onclick="window.location='.$url.'" src="'.$url.'" alt="Image Alt" width="400" height="300">'; 
    echo '</div></td>'; 
    // end tr at 3 
    if ($count == 3) { 
    echo '</tr>'; 
    // reset counter 
    $count = -1; 
    } 
    $count++; 
} 
echo '</table>'; 
+1

我認爲最好是在代碼中顯示哪些錯誤。 – SaidbakR

0

我認爲你的$count初始值有問題。

試試這個:

$count="3"; 
foreach($images as $image) { 
    if ($count%3 == 0) { 
     echo '<tr>'; 
    } 
    $count++; 

    ...