Page 1 of 1

sort by date array help ... [urgent]

Posted: Sun Aug 16, 2009 3:22 pm
by mikem562
I have the following script which generated xml for me, I need a way though so the first <image> node is the newest image, and the last node is the oldest. so instead of it doing it via filename I would like to-do it by the date it was last touched. I plan on uploading images and upon running this script I want the most recently uploaded ones at the top...I know I have to use the filemtime function to get the time of all the files in the array, but I'm still unsure how to sort them before it gets to this line here: $play_list .= "<image Thumb=\"$file_tmb\" Large=\"$file_img\"></image>\n"; ... i've tried my darnedest for the past day and I just can't get it :(

Code: Select all

<?php
   $dir = '../galleries/music/';
   $file_ext = "img.jpg";
      $play_list = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
      $play_list .= "<content>\n";
    $play_list .= "<gallery>\n";
      // Open directory, read contents and add to file_list if correct file_type
      if (is_dir($dir)) {
         if ($dh = opendir($dir)) {
               while (($file = readdir($dh)) !== false) {
                      if ($file != '.' && $file != '..') {
                         $name_array = explode('_', $file);
                            if ($name_array[1] == $file_ext) {
                               $file_img = "$file";
                  $file_tmb = str_replace("_img", "_tmb", "$file");
                            $play_list .= "<image Thumb=\"$file_tmb\" Large=\"$file_img\"></image>\n";
                            }
                         }
                   }
                closedir($dh);
               $play_list .= "</gallery>\n";
                 $play_list .= "</content>\n";
              $wdir = ''.$dir.'images1.xml';
              $f=fopen("$wdir","w") or die("Could not open and empty the file");
                 fwrite($f,$play_list) or die("Could not write to the file");
                 fclose($f) or die ("Could not close the resource");
             }
       }
?>

Re: sort by date array help ... [urgent]

Posted: Sun Aug 16, 2009 7:29 pm
by Christopher
You probably want to read all the records into an array of arrays (e.g. array(0 => array('filename'=>'foo.jpg', 'date'=>'2009-03-25')) ) and then use usort() to sort based on the dates.

Re: sort by date array help ... [urgent]

Posted: Sun Aug 16, 2009 7:51 pm
by mikem562
I'm not quite sure how to-do that. I do flash not really PHP and the above script I have took me like 2days to piece together / read etc... would you be able to give me a simple example and then I can try to work it in:)

Re: sort by date array help ... [urgent]

Posted: Sun Aug 16, 2009 8:59 pm
by Christopher
To build the array in you loop, you would do something like:

Code: Select all

$files['filename'] = $file;
$files['timestamp'] = fileatime("$dir/$file");     // see the docs
The use the usort() method to sort. See the example in the documentation that I gave you the link to above. But in your case, you want to compare ($a['timestamp'] == $b['timestamp']) and ($a['timestamp'] < $b['timestamp']).

Re: sort by date array help ... [urgent]

Posted: Mon Aug 17, 2009 1:15 am
by mikem562
well it's 2:14am here, i've been working with both sets of these codes for the past 5hours or so, and I can yet to figure it out. I think I've got the array being built correctly with the

Code: Select all

$files['filename'] = $file;
$files['timestamp'] = fileatime("$dir/$file");     // see the docs
code, yet I still can't get it to function correctly, if anyone has the time to throw it into my script correctly and post it that would be appreciated, until then I'll keep at it and let you know of my progress :) Thanks again everyone!

Re: sort by date array help ... [urgent]

Posted: Mon Aug 17, 2009 1:32 pm
by mikem562
Awesome, thank you for the last post, with that and the array link page I was able to figure it out and get it working perfectly! Thank you again!