1

我有一个项目列表,每个项目我必须显示 3 个 img。我的代码:

$path = "works/";
$dont_show = Array("", "php", ".", "..");
$dir_handle = @opendir($path) or die("Error");

while($row = mysqli_fetch_array($results)){
    echo '<li>
        <span>'.utf8_encode($row["client"]).'</span>
        <ol>';
            while ($file = readdir($dir_handle)){
                $pos = strrpos($file,".");
                $extension = substr($file, $pos);

                if (!in_array($extension, $dont_show)) {
                    echo '<li><img src="'.$path . $file.'" /></li>';
                }
            }

            closedir($dir_handle);
        echo '</ol>
    </li>';
}

所以,我试图垂直展示我的项目,并水平展示每个项目的图像。但我找不到解决方案,第二次不起作用......谢谢,我为我的英语道歉。

4

3 回答 3

0

你最好这样做:

<?php
    $path = "works/";
    $dont_show = array("", "php", ".", "..");
    $dir_handle = @opendir($path) or die("Error");

    // Store the file list from folder (but only the accepted ones)
    $file_list = array();
    while (($file = readdir($dir_handle)) !== false) {
        $ext = pathinfo($file, PATHINFO_EXTENSION);
        if (!in_array($ext, $dont_show)) array_push($file_list, $file);
    }
    closedir($dir_handle);

    // Now do your while loops
    while($row = mysqli_fetch_array($results)){
        echo "<li><span>" . utf8_encode($row['client']) . "</span><ol>";

        foreach ($file_list AS $file) { // Loop stored values
            echo "<li><img src=\"{$path}{$file}\" alt=\"\" /></li>";
        }

        echo "</ol>";
    }
?>

请注意 usingecho "text {$variable} text"与 using 相同echo "text " . $variable . " text"

于 2013-05-27T07:18:17.330 回答
0

出于某种未知原因,您一直试图显示一个目录中的文件,而您显然需要不同的文件。

因此,您必须每次都在循环opendir() 内移动并动态创建相应的项目图像目录。$path

于 2013-05-27T07:42:57.593 回答
-1
  /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($dir_handle))) {

    }

    /* This is the WRONG way to loop over the directory. */
    while ($file = readdir($dir_handle)) {

    }

为什么?我们正在明确测试返回值是否与 FALSE 相同,否则,任何名称评估为 FALSE 的目录条目都将停止循环(例如,名为“0”的目录)。

http://php.net/manual/en/function.readdir.php

于 2013-05-27T07:03:06.997 回答