jleinbaugh Posted January 24, 2009 Share Posted January 24, 2009 the following is a bit of code from a module that creates a list from the contents of a folder... how do I get it to sort the contents alphanumerically before outputting the text? $dh = opendir($name_subdir); while(gettype($file = readdir($dh))!=boolean){ if ($file != '..' && $file != '.'){ $current_element = $name_subdir.$file; if(is_dir($current_element)){ read_dir($current_element); $text .=CreatePlaylist($current_element);//Create playlist from current folder and add to output } } } closedir($dh); return $text; Quote Link to comment https://forums.phpfreaks.com/topic/142248-solved-readdir-alphanumeric-sort/ Share on other sites More sharing options...
rhodesa Posted January 24, 2009 Share Posted January 24, 2009 if you are using PHP5 or higher, you can use http://us.php.net/scandir ...if not, you have to read everything into an array and then sort. on the scandir() doc above, there is an example of that Quote Link to comment https://forums.phpfreaks.com/topic/142248-solved-readdir-alphanumeric-sort/#findComment-745221 Share on other sites More sharing options...
.josh Posted January 24, 2009 Share Posted January 24, 2009 < php5 $list = glob("*"); sort($list); Quote Link to comment https://forums.phpfreaks.com/topic/142248-solved-readdir-alphanumeric-sort/#findComment-745226 Share on other sites More sharing options...
rhodesa Posted January 24, 2009 Share Posted January 24, 2009 < php5 $list = glob("*"); sort($list); but then again...if you are not using at least PHP5, i would look into upgrading Quote Link to comment https://forums.phpfreaks.com/topic/142248-solved-readdir-alphanumeric-sort/#findComment-745229 Share on other sites More sharing options...
jleinbaugh Posted January 24, 2009 Author Share Posted January 24, 2009 I'm using php 5.28 - I just don't have any idea how to program - I using a shareware CMS (e107) and just know enough to hack a few things to make it work like I want. could you be so kind to post the fix to the above code? I just don't know enough about php to get where you're going with scandir and arrays. My knowledge is about as deep as "Why can't I just do sort($text) before it gets returned? Quote Link to comment https://forums.phpfreaks.com/topic/142248-solved-readdir-alphanumeric-sort/#findComment-745231 Share on other sites More sharing options...
.josh Posted January 24, 2009 Share Posted January 24, 2009 I'm using php 5.28 - I just don't have any idea how to program - I using a shareware CMS (e107) and just know enough to hack a few things to make it work like I want. could you be so kind to post the fix to the above code? I just don't know enough about php to get where you're going with scandir and arrays. My knowledge is about as deep as "Why can't I just do sort($text) before it gets returned? You can't just sort($text) because $text is a string. sort is for sorting a list of strings, not a single string. The way you have your code setup, it's building a string by pasting the new file name to the end of the string, so you're doing for example $text = "a"; $text = "ab"; $text = "abc"; etc... but then again...if you are not using at least PHP5, i would look into upgrading meh. unless you're heavy into oop, there isn't anything significant in php5 that you can't live without or at least mimic, in php4. And even if you're heavy into oop, there's still workarounds for a lot of the stuff. In short, it really isn't that big of a deal. Now, when php6 is released, then I'd for sure say you need to upgrade, as a lot of deprecated stuff in php4 will be gone. Quote Link to comment https://forums.phpfreaks.com/topic/142248-solved-readdir-alphanumeric-sort/#findComment-745233 Share on other sites More sharing options...
jleinbaugh Posted January 26, 2009 Author Share Posted January 26, 2009 anyone have a little time to post the code snippet I need? Quote Link to comment https://forums.phpfreaks.com/topic/142248-solved-readdir-alphanumeric-sort/#findComment-746492 Share on other sites More sharing options...
.josh Posted January 26, 2009 Share Posted January 26, 2009 There are more than enough information and links provided to do what you want. We aren't here to write your code for you. Quote Link to comment https://forums.phpfreaks.com/topic/142248-solved-readdir-alphanumeric-sort/#findComment-746506 Share on other sites More sharing options...
jleinbaugh Posted January 26, 2009 Author Share Posted January 26, 2009 sorry I offended. thought I made it clear I'm not a programmer. Quote Link to comment https://forums.phpfreaks.com/topic/142248-solved-readdir-alphanumeric-sort/#findComment-746550 Share on other sites More sharing options...
.josh Posted January 26, 2009 Share Posted January 26, 2009 sorry I offended. thought I made it clear I'm not a programmer. I'm not offended. I was just telling you how it works around here in a non-sugar coated, dull and boring way. I'm not a mechanic. Does that mean I can take my car somewhere and get it fixed for free? We are willing and happy to help you work through something if you are making an effort to work through it. You made it pretty clear that you were a beginner. You didn't really specify whether you were trying to learn or don't care about programming, you just want it done. If you aren't a programmer and don't really have a desire to get into it...well, that's when you pay someone else to do it for you. Quote Link to comment https://forums.phpfreaks.com/topic/142248-solved-readdir-alphanumeric-sort/#findComment-746604 Share on other sites More sharing options...
jleinbaugh Posted January 26, 2009 Author Share Posted January 26, 2009 trying to learn, mostly by reverse-engineering existing modules. still hurts my head. oh for the glory days when you wrote a basic program and then saved it to your tape recorder! I actually found the solution to my problem. it was farther down the script after an e_QUERY that I could sort the data. so if someone wants to mark this thread as solved that would be fine. Quote Link to comment https://forums.phpfreaks.com/topic/142248-solved-readdir-alphanumeric-sort/#findComment-746646 Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 trying to learn, mostly by reverse-engineering existing modules. still hurts my head. oh for the glory days when you wrote a basic program and then saved it to your tape recorder! I actually found the solution to my problem. it was farther down the script after an e_QUERY that I could sort the data. so if someone wants to mark this thread as solved that would be fine. You have to do that, bottom left hand corner above the "Quick Reply". Quote Link to comment https://forums.phpfreaks.com/topic/142248-solved-readdir-alphanumeric-sort/#findComment-746650 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.