TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Sorting array of folder list (http://www.talkphp.com/general/5173-sorting-array-folder-list.html)

Peuplarchie 12-16-2009 01:23 AM

Sorting array of folder list
 
Good day to you all,
I'm working on a script thta list folder in a specific folder.
My problem is that it won't sort.

Here is my code :


PHP Code:



    
        
echo '<div style="width:620px; margin-left:auto; margin-right:auto; padding:0px; ">';
$dirr="Categories/".$cat."/".$val."/";
$dh  opendir($dirr);
while (
false !== ($filename readdir($dh))) {
if (
$filename != "." && $filename != "..") { // remove the move up directory commands. (. and ..)
    
$files[] = $filename;

echo 
'<div style=" width:620px;  margin-left:auto; margin-right:auto; color:#FFFFFF; ">';
echo 
'<div style=" width:520px; margin-left:auto; margin-right:auto; color:#FFFFFF; float:left;"><img src="snippet_icon.png" border="0"/> '.$filename.'</div><br style="font-size:2px;"/>';
echo 
'<div style=" width:100px;  margin-left:auto; margin-right:auto; color:#FFFFFF; float:right; text-align:center;">';
echo 
'<a href="view_snippets.php" onclick="load(\'view_snippets.php?cat='.$cat.'&lang='.$val.'&code='.$filename.'\',\'page\');return false;" title="View Snippet '.$filename.'"><img src="view_icon.png" border="0" title="View Snippet '.$filename.'"/></a>';
echo 
'</div></div><br>';
}
}
sort ($files); // sort the file list in the array


echo "</div>";

echo 
"<br/><br/>"

Thanks!

etoolbox 12-16-2009 01:42 AM

It will correctly sort the data in the $files array. But you are not using the $files array to render your data as HTML so the rendered output is not sorted.

Do this instead:
PHP Code:

while (false !== ($filename readdir($dh))) {
    
$files[] = $filename;
}
sort($files);

foreach(
$files as $filename) {
    
// now render the html here




All times are GMT. The time now is 07:21 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0