重复 PHP readdir 代码
也许这是一个愚蠢的问题,但我自己无法解决。
我有以下代码:
<?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-index 值,如下所示:
<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>
(...)
我该怎么做?
我希望这很简单,并且您能帮助我。
谢谢。
Maybe it's a dumb question, but I wasn't able to solve it myself.
I have the following code:
<?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);
?>
As you can see, it read all files in a folder, and generates the following code:
<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>
I just need to re-run the code a few times and generate all those dinamically generated divs, in the same order again, but ALWAYS decreasing the left margin and z-index values, like this:
<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>
(...)
How do I do that?
I hope it's simple and that you can kindly help me.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要重新运行代码,您始终可以再次执行代码。
除此之外,您需要将当前的全局变量初始化从函数内移动到全局范围,以允许在代码的额外重新运行时修改它们。
这是您修改后的代码以及一些注释:
To re-run the code, you can always excute the code again.
In addition to that you need to move your current global variables initialization from within the function to the global scope to allow them to be modified at the additional re-runs of your code.
Here is your modified code with some comments: