jengels Posted February 11, 2010 Share Posted February 11, 2010 Hello everyone. I've very new to php. I have some code here I'm trying to sort the list that it creates for me of the files in a directory. I'm not having much luck and hope someone could help me. Sorting by Alpha would be fine. $ignore = array( '.', '..', '_vti_cnf', 'index.php', 'InstructSheet.txt', 'Edit Instructions Sheet.php', 'upload.php', 'admin', 'uploadstandards.php' ); if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if (!in_array ($file, $ignore)) { echo "\t<li>\n\t\t<a href=\"$file\">$file</a>\n<ul>$buffer</ul>\n\t</li>\n"; } } closedir($handle); } Quote Link to comment https://forums.phpfreaks.com/topic/191770-sorting/ Share on other sites More sharing options...
sader Posted February 11, 2010 Share Posted February 11, 2010 sort($array); U may be interested also to: arsort(), asort(), ksort(), natsort(), natcasesort(), rsort(), usort(), array_multisort(), and uksort(). Have u php manual? Quote Link to comment https://forums.phpfreaks.com/topic/191770-sorting/#findComment-1010768 Share on other sites More sharing options...
jengels Posted February 11, 2010 Author Share Posted February 11, 2010 Where do I put the: sort($array); Can you recommend a good php manual? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/191770-sorting/#findComment-1010769 Share on other sites More sharing options...
sader Posted February 11, 2010 Share Posted February 11, 2010 http://php.net/docs.php look here u can use online manual or u can download it in format such .chm put before if statement sort($ignore); if() { } Quote Link to comment https://forums.phpfreaks.com/topic/191770-sorting/#findComment-1010775 Share on other sites More sharing options...
PFMaBiSmAd Posted February 11, 2010 Share Posted February 11, 2010 In order to sort the actual file names, you must first put them into an array. You can use code similar to this recent thread - http://www.phpfreaks.com/forums/index.php/topic,287388.0.html (just remove the call to the array_reverse() function.) Quote Link to comment https://forums.phpfreaks.com/topic/191770-sorting/#findComment-1010778 Share on other sites More sharing options...
jengels Posted February 11, 2010 Author Share Posted February 11, 2010 Thank you for the replies. I think I'm still missing something. I've tried to setup an array for the files. I think I'm still missing something $ignore = array( '.', '..', '_vti_cnf', 'index.php', 'InstructSheet.txt', 'Edit Instructions Sheet.php', 'upload.php', 'admin', 'uploadstandards.php' ); $file = array(); sort ($file); if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if (!in_array ($file, $ignore)) { echo "\t<li>\n\t\t<a href=\"$file\">$file</a>\n<ul>$buffer</ul>\n\t</li>\n"; } } closedir($handle); } Quote Link to comment https://forums.phpfreaks.com/topic/191770-sorting/#findComment-1010818 Share on other sites More sharing options...
sader Posted February 11, 2010 Share Posted February 11, 2010 I looked a bit careful at your code heres how it shod go $ignore = array('.', '..', '_vti_cnf', 'index.php', 'InstructSheet.txt', 'Edit Instructions Sheet.php', 'upload.php', 'admin', 'uploadstandards.php'); $result_array = array(); if ($handle = opendir('.')) { while(false !== ($file = readdir($handle))) { if(!in_array ($file, $ignore)) { $result_array[] = $file; } } closedir($handle); } sort($result_array); foreach($result_array as $file) { echo "\t<li>\n\t\t<a href=\"$file\">$file</a>\n<ul>$buffer</ul>\n\t</li>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/191770-sorting/#findComment-1010825 Share on other sites More sharing options...
jengels Posted February 11, 2010 Author Share Posted February 11, 2010 Thank you so much! I have a lot to learn Quote Link to comment https://forums.phpfreaks.com/topic/191770-sorting/#findComment-1010836 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.