-2

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 enter image description here

the actual result enter image description here

Anantha Raju C
  • 1,780
  • 12
  • 25
  • 35
Bjorn Pijpops
  • 323
  • 3
  • 7
  • 19
  • 1
    Before you print the filenames put them into an array and sort it as you want(See: http://stackoverflow.com/q/17364127/3933332). – Rizier123 Nov 08 '16 at 00:13
  • http://stackoverflow.com/questions/2320024/are-there-any-php-code-visualization-tools –  Nov 08 '16 at 00:13
  • 1
    First off, it is best to actually put the code in the question instead of linking to an image. Second, you are using `
    ` instead of ``.
    – esote Nov 08 '16 at 00:13
  • There is a inside my
    , isn't that ok?
    – Bjorn Pijpops Nov 08 '16 at 00:18
  • I dont' fully understand why I need to put it in an array and sort it, if the files (in this case pdf's) are in the correct order shouldn't it just read them in correctly? – Bjorn Pijpops Nov 08 '16 at 00:20
  • @BjornPijpops no it's not ok – Jim Nov 08 '16 at 00:20
  • @Jim could you explain why that is not ok? – Bjorn Pijpops Nov 08 '16 at 00:21
  • @BjornPijpops where specifically are you seeing them ordered ? On the FTP ? They are ordered based on the UI grid ordering system. The files you are retrieving are never specifically ordered. You have to order them for yourself before you output them as Rizier123 mentioned. – Jim Nov 08 '16 at 00:23
  • @Jim Yes they are ordered in the FTP so I figured that when I would use readdir, it would maintain the same order which it clearly doesn't but the explanation as to why it doesn't was given by starnzd. – Bjorn Pijpops Nov 08 '16 at 00:27

1 Answers1

0

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

For reference:

genesst
  • 1,333
  • 1
  • 10
  • 39