Bjorn Pijpops Bjorn Pijpops - 1 year ago 54
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 Source

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

For reference: