Sorting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jgmcelwain
Forum Newbie
Posts: 4
Joined: Thu Aug 12, 2010 11:12 am

Sorting

Post 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>";
       }
} ?>
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Sorting

Post 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?
jgmcelwain
Forum Newbie
Posts: 4
Joined: Thu Aug 12, 2010 11:12 am

Re: Sorting

Post by jgmcelwain »

Files are named 1.txt, 2.txt, 3.txt etc.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Sorting

Post 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 ?
jgmcelwain
Forum Newbie
Posts: 4
Joined: Thu Aug 12, 2010 11:12 am

Re: Sorting

Post by jgmcelwain »

20.txt comes after 2.txt. GLOB_NOSORT didn't do anything. :S
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Sorting

Post 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 :)
jgmcelwain
Forum Newbie
Posts: 4
Joined: Thu Aug 12, 2010 11:12 am

Re: Sorting

Post by jgmcelwain »

Ok, thanks for pointing me in the right direction. :D
Post Reply