Php 库自动生成表

Php 库自动生成表,php,Php,我有一个脚本,调用文件夹中的所有照片并列出它们,但现在该脚本列出了同一行中的所有照片 我需要把它们放在一张桌子上,比如: <table> <tr><td>photo 1</td><td>photo 2</td><td>photo 3</td></tr> <tr><td>photo 4</td><td>photo 5</td><

我有一个脚本,调用文件夹中的所有照片并列出它们,但现在该脚本列出了同一行中的所有照片

我需要把它们放在一张桌子上,比如:

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

照片1照片2照片3
照片4照片5照片6
照片7照片8照片9
这样一直持续到没有更多的照片。显示照片的代码如下所示:

<?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>';
    }   
}
?>


有人能帮我解决这个问题吗?

应该是这样的:

<?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>";

如果您真的想使用PHP执行此操作并将其呈现为HTML:

创建包含所有图像的阵列:

$images = [];
foreach(dirArray as $file) {
    $extension = substr($file, -3);
    if ($extension == ('jpg' | 'JPG')){
        $images[] = $file;
    }
}
然后可以从该数组中创建块(

然后渲染您的表:

$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;
$output='';
$i=0;
foreach($行作为$行){
$output.='';
foreach($行作为$文件)
{
$output.=''.$file';
}
//添加额外的空单元格
如果($i==count($rows)-1){
$output.=str_repeat(“”,$columns-count($row));
}
$output.='';
$i++;
}
$output.='';
echo$输出;
我宁愿不使用表格,而是使用响应网格系统()。
您仍然可以使用上面的代码,但可以使用正确的类将表部分替换为。

这里的想法是,您尝试做您想做的事情,然后如果您有问题,您可以问一个关于您的问题的问题。您不会显示一段完全无关的代码,并期望我们为您完成所有的工作。您可以提供
我有一个scr吗ipt调用文件夹中的所有照片并将其列出。但现在脚本将在同一原始文件中列出所有照片。
?如果您向我们展示您的实际问题(您拥有的与您想要的)我们更有可能帮助您。请注意我上面的评论。回答布拉特为我做的问题只会鼓励更多类似的问题请注意我上面的评论。回答布拉特为我做的问题只会鼓励更多类似的问题。但不是我的DV
$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;