Jump to content

Sorting values


inka

Recommended Posts

Greetings!

 

I would like to ask for you help sorting a couple of lists that are autogenerated with php.

The page reads a content of a folder and makes a menu from the names. The folder names are: a, b, c, d... etc.

 

The other part reads the content of the selected folder and displays the files.

 

My problem is, that on windows, using xampp, the both the folders and files appear in alphabetic order (what I want), but on the linux server they get completely messed up.

Instead of a, b, its o, a, d, etc.

I tried using the sort() function but there is no array to sort...

 

Thank you for any suggestions and help!

Inka

 

ps. the server has php 4.3.9... does this matter? could help if we upgrade to 5.3?

 

here is the code:

<?php     
     $dh  = opendir($dir);

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

       $files[] = $filename;

        if ($filename != "." && $filename != "..")
                     {    
        if($_SERVER['REQUEST_URI'] == "index.php?l=" . $filename . '&sec=' . $sec){
                     echo '<td><img src="imagini/arow.gif"></td><td style="text-align: left;">' . strtoupper($filename) . $space;
                     } else {    

            echo '<td style="text-align: left;"><a href="index.php?l='. $filename . '&sec=' . $sec . '">' . strtoupper($filename) . $space;                }}}

?>

<?php

    $letter = "a";

    if(isset($_GET["l"])){

    $letter = $_GET["l"];} 

    $letter = $dir . $letter;

if ($handle = opendir($letter)) {

   while (false !== ($file = readdir($handle)))

      {

          if ($file != "." && $file != "..")

      {

     $filename = $file;
     $file2 = substr($filename, 0, -4);
     $thelist .= '<tr><td style="width: 250px; text-align: left;"><a href="'. $letter . '/'. $file.'">'.$file2.'</a></td></tr>';

          }
       }
  closedir($handle);
  }
?>
<p><?php echo $thelist; ?></p>

Link to comment
https://forums.phpfreaks.com/topic/215415-sorting-values/
Share on other sites

Create an array to sort. Rather than:

 

while (false !== ($file = readdir($handle))) {
    if ($file != "." && $file != "..") {
        $filename = $file;
        $file2 = substr($filename, 0, -4);
        $thelist .= '<tr><td style="width: 250px; text-align: left;"><a href="'. $letter . '/'. $file.'">'.$file2.'</a></td></tr>';
    ...snip...
    }
}

Try:

 

$list_items = array();
while (false !== ($file = readdir($handle))) {
    if ($file != "." && $file != "..") {
        $filename = $file;
        $file2 = substr($filename, 0, -4);
        $list_items[$file2] = $letter."/".$file;
    }
}
sort($list_items);
foreach($list_items as $key => $val) {
    $thelist .= "<tr><td style=\"width: 250px; text-align: left;\"><a href=\"".$val."\">".$key."</a></td></tr>";
}

Untested, but you get the idea.

Link to comment
https://forums.phpfreaks.com/topic/215415-sorting-values/#findComment-1120150
Share on other sites

Thank you Pawn!

 

It almost worked, but unfortunately the array key was not changed, was still 0-1-2 etc...

But I managed to modify the $val to get the name I wanted.

 

To sort the menu, I used ksort to sort the value not the key, but finally it worked, even its a very messy code  ::)

 

Thanks again!

Inka

 

Link to comment
https://forums.phpfreaks.com/topic/215415-sorting-values/#findComment-1120203
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.