Nintendo-Daily Posted June 21, 2009 Share Posted June 21, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/163074-solved-sorting-issue/ Share on other sites More sharing options...
Nintendo-Daily Posted June 21, 2009 Author Share Posted June 21, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/163074-solved-sorting-issue/#findComment-860499 Share on other sites More sharing options...
.josh Posted June 21, 2009 Share Posted June 21, 2009 where are the category names coming from? what are you doing to sort it? How are we supposed to even begin to help you when you don't explain these things or post code? Quote Link to comment https://forums.phpfreaks.com/topic/163074-solved-sorting-issue/#findComment-860511 Share on other sites More sharing options...
Nintendo-Daily Posted June 21, 2009 Author Share Posted June 21, 2009 The category names are coming from the folders I create. Each folder I create is a category. Nothing is being done to sort it so I am assuming it's using PHPs default sorting method. Hope this helps... Quote Link to comment https://forums.phpfreaks.com/topic/163074-solved-sorting-issue/#findComment-860514 Share on other sites More sharing options...
.josh Posted June 21, 2009 Share Posted June 21, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/163074-solved-sorting-issue/#findComment-860519 Share on other sites More sharing options...
DaveLinger Posted June 21, 2009 Share Posted June 21, 2009 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>"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/163074-solved-sorting-issue/#findComment-860521 Share on other sites More sharing options...
.josh Posted June 21, 2009 Share Posted June 21, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/163074-solved-sorting-issue/#findComment-860675 Share on other sites More sharing options...
Nintendo-Daily Posted June 22, 2009 Author Share Posted June 22, 2009 Thanks for your efforts, Crayon Violent. Also, DaveLinger did a wonderful job implementing this into the pages A+ support to both of you!! Quote Link to comment https://forums.phpfreaks.com/topic/163074-solved-sorting-issue/#findComment-860944 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.