2013-05-07 31 views
0

我必須製作一個可以通過變量($ cols)設置的列數量,每個單元格包含通過GLOB數組獲取的圖片。我現在的代碼將輸出一個正確數量的單元格和列的表格,但我需要幫助讓每張圖片都顯示出來。如何使這個可變大小的表格從GLOB數組輸出圖像?

<?php 
$cols = 4; 

$array = glob("include/*.{jpg}", GLOB_BRACE); 


$output = "<table>\n"; 

$cell_count = 1; 

for ($i = 0; $i < count($array); $i++) { 
    if ($cell_count == 1) { 
     $output .= "<tr>\n"; 
    } 
    $output .= "<td><img src=$array></td>\n"; 
    $cell_count++; 

    if ($cell_count > $cols || $i == (count($array) - 1)) { 
     $output .= "</tr>\n"; 
     $cell_count = 1; 
    } 
} 
$output .= "</table>\n"; 
echo "$output"; 

?> 

回答

0

您不是索引到數組中以獲取單個項目。這:

$output .= "<td><img src=$array></td>\n"; 

應該

$output .= "<td><img src=\"$array[$i]\"></td>\n"; 

還請注意,我在逃避雙引號,這樣你的HTML src屬性值是雙引號括起來。

此外,您還可以使聲明更有效,如果您緩存數($陣列)另一個變量,雖然它可能不是一個大問題。

+0

哇,我不能相信我錯過了。謝謝! – user2359675 2013-05-07 20:09:32

相關問題