我有一个项目的清单,我必须显示每个项目3IMG。我的代码:
$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>';
}所以,我试着垂直显示我的项目,并水平显示每个项目的图像。但是我找不到解决的办法,第二种方法不起作用……谢谢,我为我的英语道歉。
发布于 2013-05-27 15:18:17
你最好这样做:
<?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>";
}
?>请注意,使用echo "text {$variable} text"与使用echo "text " . $variable . " text"相同。
发布于 2013-05-27 15:42:58
出于某些未知的原因,您试图始终显示来自单个目录的文件,而您显然需要不同的文件。
因此,您必须在循环中移动opendir(),并每次为相应的项目图像目录动态创建$path。
发布于 2013-05-27 15:03:07
/* 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
https://stackoverflow.com/questions/16766082
复制相似问题