2016-12-20 68 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