Bjorn Pijpops Bjorn Pijpops - 1 month ago 8
HTML Question

While loop, not looping in the correct order

Disclaimer: I did not write this code myself a friend gave it to me.

<?php
$handle = opendir(dirname(realpath(__FILE__)).'/AlgemeneVergaderingen/');
while($file = readdir($handle)){
if($file !== '.' && $file !== '..'){
echo '<p><a href="AlgemeneVergaderingen/' .$file.'">' .$file. '</a></p>';
}
}
?>


The point of this code is to loop inside a map and get all the files and echo them out IN THE ORDER that they are inside of the map.

The code does echo them but not in the correct order.

below 2 pictures to help visualize.

full code/file structure

the actual result

Answer

readdir() doesn't guarantee any order. You need to order it yourself.

For reference: