Repeat a PHP readdir code

354 views Asked by At

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.

1

There are 1 answers

2
favo On BEST ANSWER

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:

<?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);
?>