Sorting problem with most recent uploaded file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
northernlight_10
Forum Newbie
Posts: 3
Joined: Thu Jul 09, 2009 4:41 am

Sorting problem with most recent uploaded file

Post by northernlight_10 »

Hi,

I'm using the following code to show a list of files. The files should be sorted by the most recent date.

Code: Select all

 <?php
 $recent = $site_class->data_get(db_productfiles,'','','pfile_date_created DESC','500');
 $n=1;
 $pout = array();
 $monthlist = array ('01' => 'January', '02' => 'February', '03' => 'March', '04' => 'April', '05' => 'May', '06' => 'June', '07' => 'July', '08' => 'August', '09' => 'September', '10' => 'October', '11' => 'November', '12' => 'December');
 while (list($name, $value) = each($recent)) {
 //foreach ($recent as $name => $value){
  $time = $recent[$name]['pfile_date_created'];
  list ($year,$month) = explode("-",$time);
  $time = $month."-".$year;
    
  if (empty($pout[$time])){
   $pout[$time] = "<strong>".$monthlist[$month]."-".$year."</strong><br />"; 
   
  }
  $pout[$time] = $pout[$time]."<a href=\"PDF/Test_PDF/".$recent[$name]['pfile_file']."\"class=\"a\">".$recent[$name]['pfile_description']."</a> - ".$recent[$name]['pfile_type']." - ".$recent[$name]['pfile_size']."Kb<br />";
  
  $n++;
 }
 krsort($pout);
 
 foreach ($pout as $name => $value){
  echo ucfirst($value)."<br />";  
 }
}
?>
This was working correctly in 2009, but since this year, there's a problem with sorting. The previous upload is from September 2009, and this is the first item on the page. In January 2010, there where some new files uploaded, and they are shown at the bottom of the page, just above January 2009.

Could someone help me how to modify the code to sort by year and then month, so that January 2010 shows on top of the page?
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Sorting problem with most recent uploaded file

Post by JakeJ »

Your date stamp is taking the month first. Reverse them.

Code: Select all

 
<?php
 
//foreach ($recent as $name => $value){
 $time = $recent[$name]['pfile_date_created'];
 list ($year,$month) = explode("-",$time);
$time = $month."-".$year;
?>
$time is taking $month first instead of $year. Since December = 12 it will always produce Dec 2009 as the most recent date until you get to Dec 2010.

Adjust accordingly.
northernlight_10
Forum Newbie
Posts: 3
Joined: Thu Jul 09, 2009 4:41 am

Re: Sorting problem with most recent uploaded file

Post by northernlight_10 »

Thanks for the reply!
It's working correctly now.
Post Reply