Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在html表格中显示图像(如果循环)_Php - Fatal编程技术网

Php 在html表格中显示图像(如果循环)

Php 在html表格中显示图像(如果循环),php,Php,我希望编写一个PHP脚本,将目录中的图像发布到一个8列宽的表格式中,并且行可以扩展尽可能多的图像。这段当前代码我只在单独的行中发布它们。如何将它们分成8个图像行 <?php $files = glob("images/*.*"); for ($i=1; $i<count($files); $i++) { $image = $files[$i]; $supported_file = array( 'gif', 'jpg',

我希望编写一个PHP脚本,将目录中的图像发布到一个8列宽的表格式中,并且行可以扩展尽可能多的图像。这段当前代码我只在单独的行中发布它们。如何将它们分成8个图像行

<?php

$files = glob("images/*.*");
for ($i=1; $i<count($files); $i++)
{
    $image = $files[$i];
    $supported_file = array(
        'gif',
        'jpg',
        'jpeg',
        'png'
    );

    $ext = strtolower(pathinfo($image, PATHINFO_EXTENSION));
    if (in_array($ext, $supported_file)) {
        // print $image ."<br />";
        echo '<a href="' .$image .'"><img src="'.$image .'" alt="Random image" width=200 /></a>'."<br /><br />";
    } else {
        continue;
    }
}
?>

处理glob的一种更简单的方法是使用foreach。在获得正确的循环之后,您可以按任何方式自定义html输出

<?php

foreach (glob('images/*.{gif,jpg,jpeg,png}', GLOB_BRACE) as $image) {
  echo '<a href="' .$image .'"><img src="'.$image .'" alt="Random image" width=200 /></a>'."<br /><br />";
}

处理glob的一种更简单的方法是使用foreach。在获得正确的循环之后,您可以按任何方式自定义html输出

<?php

foreach (glob('images/*.{gif,jpg,jpeg,png}', GLOB_BRACE) as $image) {
  echo '<a href="' .$image .'"><img src="'.$image .'" alt="Random image" width=200 /></a>'."<br /><br />";
}

像这样的东西$i%8每第8行返回0,因此我们所做的就是停止/启动
标记

<table>
    <tr>
        <?php
        $files = glob("images/*.*");
        for ($i = 1; $i < count($files); $i++) {
            $image = $files[$i];
            $supported_file = array(
                'gif',
                'jpg',
                'jpeg',
                'png'
            );

            $ext = strtolower(pathinfo($image, PATHINFO_EXTENSION));
            if (in_array($ext, $supported_file)) {
                // print $image ."<br />";
                echo '<td><a href="' . $image . '"><img src="' . $image . '" alt="Random image" width=200 /></a></td>';
            }
            if ($i % 8 === 0) {
                echo "</tr><tr>";
            }
        }
        ?>
    </tr>
</table>

像这样的东西$i%8每第8行返回0,因此我们所做的就是停止/启动
标记

<table>
    <tr>
        <?php
        $files = glob("images/*.*");
        for ($i = 1; $i < count($files); $i++) {
            $image = $files[$i];
            $supported_file = array(
                'gif',
                'jpg',
                'jpeg',
                'png'
            );

            $ext = strtolower(pathinfo($image, PATHINFO_EXTENSION));
            if (in_array($ext, $supported_file)) {
                // print $image ."<br />";
                echo '<td><a href="' . $image . '"><img src="' . $image . '" alt="Random image" width=200 /></a></td>';
            }
            if ($i % 8 === 0) {
                echo "</tr><tr>";
            }
        }
        ?>
    </tr>
</table>


注意:在循环中,您可能希望以0开头,而不是从1开始注意:在循环中,您可能希望以0开头,而不是从1开始。循环不回答8列。不回答8列。