Jump to content

[SOLVED] Sorting Issue


Nintendo-Daily

Recommended Posts

I hope this is the correct spot to ask this question. I seem to have some kind of sorting issue going on with the way images are displayed on a page. I would like to be able to sort the images by file name but that isn't happening.

 

Any help "sorting" this issue out would be greatly appreciated :)

 

Here's the page:

http://www.nintendo-daily.com/game.php?id=1&page=screenshots&cat=World%201%20-%208

Link to comment
Share on other sites

I don't see an option for me to edit my post. I just wanted to let everyone know that the sorting for the files (screenshots) has been taken care of.

 

Sorry for the double post. I just wanted to let everyone know so no one wastes their time with this issue.

 

I do however have something else I can't figure out. Although the files sort correctly, the categories do not. Here's an example of what the categories look like:

 

http://www.nintendo-daily.com/game.php?id=1&page=screenshots

 

I've added each category (folder) in numerical order.

 

Any thoughts?

Link to comment
Share on other sites

so...are these real folders on the server or "folders" in a cms (you mentioned creating them in numerical order, whatever that's supposed to mean)?  How are you retrieving the names of them?  How are you displaying them? 

 

Don't expect anything just "goes without saying," because it doesn't.

Link to comment
Share on other sites

Basically the php code finds subfolders in a "screenshots" directory of a game's directory, then lists each subdir it finds. Currently they're not sorted in terms of what order they're being printed.

 

$dirpath = "games/{$gameid}/screens/";

$dir = opendir($dirpath);

while (($filename = readdir($dir)) !== false){



if($filename != "" && $filename != "." && $filename != ".."){

if(strpos($filename, ".") === false){

echo "<div class=\"category\"><a href=\"game.php?id={$gameid}&page=screenshots&cat={$filename}\">{$filename}</a></div>";

}

}

}

Link to comment
Share on other sites

Okay so here's the thing: It's not php's fault it's sorted that way, it's your server's.  The script you are using just reads the stuff from the directory as the server has it listed.  But anyways, you can re-order in php lots of different ways.

 

But in general, your script kind of sucks.  It's overkill and also you are checking to see if it's a directory by virtue of not having a dot in the name somewhere.  That doesn't necessarily mean it's a directory.  Here is a much better way of doing it:

 

$files = glob('*',GLOB_ONLYDIR);
sort($files);
//usort($files, create_function('$a,$b','return strcasecmp($a,$b);'));
foreach($files as $filename) {
  echo "<div class=\"category\"><a href=\"game.php?id={$gameid}&page=screenshots&cat={$filename}\">{$filename}</a></div>";
}

 

Note: the way sort() works, it sorts by ascii char comparison, so for instance, if you have this list:

 

apple

banana

Orange

 

sort() will make it like so:

 

Orange

apple

banana

 

if you want it to be case-insensitive, I included a commented out line that will do that.  just remove the sort(..) and use the commented out line instead.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.