重复PHP readdir代码
也许这是个愚蠢的问题,但我自己没能解决 我有以下代码:重复PHP readdir代码,php,html,readdir,opendir,Php,Html,Readdir,Opendir,也许这是个愚蠢的问题,但我自己没能解决 我有以下代码: <?php $path = "galeria01"; $dir_handle = @opendir($path) or die("Not found: $path"); list_dir($dir_handle,$path); function list_dir($dir_handle,$path) { global $div; $div = 001;
<?php
$path = "galeria01";
$dir_handle = @opendir($path) or die("Not found: $path");
list_dir($dir_handle,$path);
function list_dir($dir_handle,$path)
{
global $div;
$div = 001;
global $zindex;
$zindex = 200;
global $margem;
$margem = 114;
while ((($file = readdir($dir_handle)) !== false)) {
if ($file != "." && $file != ".." ) {
echo PHP_EOL . '<div id="';
echo str_pad($div, 3, 0, STR_PAD_LEFT);
echo '" style="position:absolute;left:';
echo $margem;
echo 'px;z-index:';
echo $zindex;
echo '"><img src="galeria01/';
echo $file;
echo '" width="675" height="450" /></div>';
echo'<span class="clear"></span>';
$div++;
$zindex--;
$margem = $margem - 675;
}
}
}
closedir($dir_handle);
?>
<div id="001" style="position:absolute;left:114px;z-index:200"><img src="001.jpg" width="675" height="450" /></div><span class="clear"></span>
<div id="002" style="position:absolute;left:-561px;z-index:199"><img src="002.jpg" width="675" height="450" /></div><span class="clear"></span>
<div id="003" style="position:absolute;left:-1236px;z-index:198"><img src="003.jpg" width="675" height="450" /></div><span class="clear"></span>
<div id="004" style="position:absolute;left:-1911px;z-index:197"><img src="004.jpg" width="675" height="450" /></div><span class="clear"></span></div>
如您所见,它读取文件夹中的所有文件,并生成以下代码:
<?php
$path = "galeria01";
$dir_handle = @opendir($path) or die("Not found: $path");
list_dir($dir_handle,$path);
function list_dir($dir_handle,$path)
{
global $div;
$div = 001;
global $zindex;
$zindex = 200;
global $margem;
$margem = 114;
while ((($file = readdir($dir_handle)) !== false)) {
if ($file != "." && $file != ".." ) {
echo PHP_EOL . '<div id="';
echo str_pad($div, 3, 0, STR_PAD_LEFT);
echo '" style="position:absolute;left:';
echo $margem;
echo 'px;z-index:';
echo $zindex;
echo '"><img src="galeria01/';
echo $file;
echo '" width="675" height="450" /></div>';
echo'<span class="clear"></span>';
$div++;
$zindex--;
$margem = $margem - 675;
}
}
}
closedir($dir_handle);
?>
<div id="001" style="position:absolute;left:114px;z-index:200"><img src="001.jpg" width="675" height="450" /></div><span class="clear"></span>
<div id="002" style="position:absolute;left:-561px;z-index:199"><img src="002.jpg" width="675" height="450" /></div><span class="clear"></span>
<div id="003" style="position:absolute;left:-1236px;z-index:198"><img src="003.jpg" width="675" height="450" /></div><span class="clear"></span>
<div id="004" style="position:absolute;left:-1911px;z-index:197"><img src="004.jpg" width="675" height="450" /></div><span class="clear"></span></div>
我只需要重新运行代码几次,并再次以相同的顺序生成所有这些DIV,但总是减少左边距和z索引值,如下所示:
<div id="001" style="position:absolute;left:114px;z-index:200"><img src="001.jpg" width="675" height="450" /></div><span class="clear"></span>
(...)
<div id="004" style="position:absolute;left:-1911px;z-index:197"><img src="004.jpg" width="675" height="450" /></div><span class="clear"></span></div>
<div id="001" style="position:absolute;left:-2586px;z-index:196"><img src="001.jpg" width="675" height="450" /></div><span class="clear"></span>
(...)
(...)
(...)
我该怎么做
我希望它很简单,你能帮助我
谢谢。要重新运行代码,您可以再次执行代码 除此之外,您还需要将当前全局变量初始化从函数中移动到全局范围,以允许在代码的其他重新运行时修改它们 以下是您的修改代码和一些注释:
<?php
$path = ".";
$dir_handle = @opendir($path) or die("Not found: $path");
// do the init of these variables outside of the function and before the first call
global $zindex;
$zindex = 200;
global $margem;
$margem = 114;
// now call your function, it will behave like your original code
list_dir($dir_handle,$path);
// now call the your function again, it will pick up with the zindex/margem values of the last div you printed
list_dir($dir_handle,$path);
function list_dir($dir_handle,$path)
{
global $div;
$div = 001;
global $zindex;
// no longer set a new value for the zindex with each function call
//$zindex = 200;
global $margem;
// no longer set a new value for the margem with each function call
//$margem = 114;
//reset the directory handle to the first position
rewinddir($dir_handle);
while ((($file = readdir($dir_handle)) !== false)) {
if ($file != "." && $file != ".." ) {
echo PHP_EOL . '<div id="';
echo str_pad($div, 3, 0, STR_PAD_LEFT);
echo '" style="position:absolute;left:';
echo $margem;
echo 'px;z-index:';
echo $zindex;
echo '"><img src="galeria01/';
echo $file;
echo '" width="675" height="450" /></div>';
echo'<span class="clear"></span>';
$div++;
$zindex--;
$margem = $margem - 675;
}
}
}
closedir($dir_handle);
?>
不,这毫无意义。“从1到4而不是从1到4”?嗯?(HTML id值不能以数字开头。)对于这样的事情,我通常会打电话给我的朋友。同意,你的问题毫无意义。。。也许你应该先把问题孤立起来,简化问题:hakre说了什么。那么,如果我知道您希望能够多次使用此函数,但使用不同的边距和z索引?那么,为什么不在函数中添加一个基本边距和一个基本z索引呢?不能有多个ID相同的元素。请使用类。我能够理解您的想法,完全按照您所说的做了,但遗憾的是,输出只列出了一次文件,就像以前一样。不管我调用函数多少次,它在HTML上只列出一次。这是因为dir_句柄在第一次函数调用结束后指向末尾。我修改了我的答案,在读取目录内容之前使用rewinddir(),因此始终从一开始就读取完整的目录树