Page 1 of 1

Sorting

Posted: Thu Aug 12, 2010 11:14 am
by jgmcelwain
Anyone got any ideas why this would display the first of $files, then the tenth, then the second, thrid and fourth etc. instead of the first, second, third, fourth fifth etc.?

Code: Select all

<?php function xfunction($page = 1, $num_posts = 5) {
       $start = (($page - 1) * $num_posts);
       $path = "content/posts/";
       $files = glob("" . $path . "*.txt");
       if ($files) {
               rsort($files);
               $files = array_slice($files, $start, $num_posts);

               foreach ($files as $file) {
                       print("\n<article>");
                       include($file);
                       print("</article>\n");
               }
       } else {
               echo "Somthing's Missing? Maybe you need to <a href='install.php'>Install?</a>";
       }
} ?>

Re: Sorting

Posted: Thu Aug 12, 2010 11:39 am
by mikosiko
without knowing the names of your files I'm guessing that glob is ordering them in that way.
http://php.net/manual/en/function.glob.php

did you try using the GLOB_NOSORT flag?

Re: Sorting

Posted: Thu Aug 12, 2010 11:42 am
by jgmcelwain
Files are named 1.txt, 2.txt, 3.txt etc.

Re: Sorting

Posted: Thu Aug 12, 2010 11:48 am
by mikosiko
simple test.... what happens if you have a file named 20.txt ... in which position it will came?

again... did you tried the GLOB_NOSORT ?

Re: Sorting

Posted: Thu Aug 12, 2010 12:33 pm
by jgmcelwain
20.txt comes after 2.txt. GLOB_NOSORT didn't do anything. :S

Re: Sorting

Posted: Thu Aug 12, 2010 12:48 pm
by mikosiko
jgmcelwain wrote:20.txt comes after 2.txt.
Ok... now you have something to start investigating... why that happens?

I'm not going to solve it for you... I'm just giving you clues about the reasons for your issue.... time to google :)

happy learning :)

Re: Sorting

Posted: Thu Aug 12, 2010 12:57 pm
by jgmcelwain
Ok, thanks for pointing me in the right direction. :D