2016-12-20 128 views
-3

我有一個腳本調用文件夾中的所有照片並列出它們,但現在腳本列出了同一行中的所有照片。圖庫自動生成表

我需要他們被放置在一個表,如:

<table> 
<tr><td>photo 1</td><td>photo 2</td><td>photo 3</td></tr> 
<tr><td>photo 4</td><td>photo 5</td><td>photo 6</td></tr> 
<tr><td>photo 7</td><td>photo 8</td><td>photo 9</td></tr> 
</table> 

並繼續這樣,直到有沒有更多的照片。顯示照片的代碼如下所示:

<?php 
// loop through the array of files and print them all in a list 
for($index=0; $index < $indexCount; $index++) { 
    $extension = substr($dirArray[$index], -3); 
    if ($extension == ('jpg' | 'JPG')){ // list only jpgs and JPG 

     echo '<li><img src=" thumbnail/' . $dirArray[$index] . '" alt="Image" /><span>' . $dirArray[$index] . '</span>'; 
    } 
} 
?> 

有些人可以幫我解決這個問題嗎?

+1

這裏的想法是,你嘗試做你想做的事情,然後如果你有問題,你問一個關於你的問題的問題。你**不**顯示**完全無關的代碼**,並期望我們爲你**所有工作** – RiggsFolly

+0

你能提供'我有一個腳本,調用一個文件夾中的所有照片,並列出他們。但是現在這個腳本列出了所有的照片都是原始的。如果你向我們展示你的實際問題(你有什麼和你想要的),我們更有可能幫助你。 – Guildencrantz

回答

0

這將是類似的東西:

<?php 
echo "<table>"; 
$column = 0; 
$closed = false; 
// loop through the array of files and print them all in a list 
for($index=0; $index < $indexCount; $index++) { 
$extension = substr($dirArray[$index], -3); 
if ($extension == ('jpg' | 'JPG')){ // list only jpgs and JPG 
if($column === 0) { 
    echo "<tr>"; 
    $closed = false; 
} 
echo '<td><img src=" thumbnail/' . $dirArray[$index] . '" alt="Image" /><span>' . $dirArray[$index] . '</span></td>'; 
$column++; 
if($column === 3) { 
    echo "</tr>"; 
    $column = 0; 
    $closed = true; 
} 
} 
} 

if(!$closed) { 
    echo "</tr>"; 
} 
echo "</table>"; 
+1

請注意我上面的評論。回答blatent **爲我做的**問題只會鼓勵更多這樣的問題 – RiggsFolly

-1

如果你真的想用PHP來做到這一點,它呈現爲HTML:

創建的所有圖像的數組:

$images = []; 
foreach(dirArray as $file) { 
    $extension = substr($file, -3); 
    if ($extension == ('jpg' | 'JPG')){ 
     $images[] = $file; 
    } 
} 

然後您可以創建該陣列中的塊(http://php.net/manual/en/function.array-chunk.php

$columns = 3; 
$rows = array_chunk($images,$columns); 

然後渲染你的表:

$output = '<table>'; 
$i = 0; 
foreach ($rows as $row) { 
    $output .= '<tr>'; 
    foreach($row as $file) 
    { 
     $output .= '<td><img src="thumbnail/' . $file . '" alt="Image" /><span>' . $file . '</span></td>'; 
    } 

    //Add extra empty cells 
    if ($i === count($rows) - 1) { 
      $output .= str_repeat('<td>&nbsp;</td>', $columns - count($row)); 
    } 
    $output .= '</tr>'; 

    $i++; 
} 
$output .= '</table>'; 

echo $output; 

我寧願不與表工作,但使用響應電網系統(http://www.responsivegridsystem.com/)。 您仍然可以使用上面的代碼,但用正確的類替換表格部件。

+0

請注意我上面的評論。回答blatent **爲我做這個**問題只會鼓勵更多這樣的問題。雖然不是我的DV – RiggsFolly