dodanie sortowania

0

Znalazłem taki kod, który mi wyświetla obrazy z katalogu, ale chciałbym żeby wyniki były posortowane od najstarszego do najnowszego. Pomożecie? ;)

 <table cellpadding="4" cellspacing="0" width="760"> 
<?php 
    $dir   = './'; // base directory for thumbnails 
    $large = './'; // sub-directory from base-directory in which full images are held 
    $allow = array('jpg','jpeg', 'JPEG', 'JPG'); 

    $i=0; 
    $open = opendir($dir); 
    // get each filename from directory 
    while (($file=readdir($open))!==false) { 
        // get extension 
        $ext=str_replace('.', '', strrchr($file, '.')); 
        // does it have a valid extension 
        if (in_array($ext, $allow))  
          $list[$i++]=$file; // store valid filename in array. use numerical indexing as makes it easier to display paginated images later 
    } 
     
    $perPage=12; // number of images to show per page 
    $total=count($list); // total number of images to show 
    $pages=ceil($total/$perPage); // number of pages is the number of images divided by how many per page 

    $thisPage=isset($_GET['pg'])?$_GET['pg']-1:0; // did user select a specific page? Note, pages on web are counted from 1 (not zero) so must subtract 1 for correct indexing 
    $start=$thisPage*$perPage; // calculate starting index into list of filenames 
     
    $perRow=2; // how many images to be shown on each row 
     
    // display quick index to pages. all pages except current page output as a link 
    print "Page "; 
    for ($i=0;$i<$pages;$i++) 
      if ($i==$thisPage) 
        print "&nbsp;".($i+1); 
      else 
        print "&nbsp;<a href='?pg=".($i+1)."'>".($i+1)."</a>"; 
         
    print "<tr>"; 
    $imgCnt=0; // used to count number of images displayed and hence whether to wrap page. note, could use "for" index $i but this is computationally quicker 
    for ($i=$start;$i<$start+$perPage;$i++) { 
      // may be too few images to fill page, so check if we have a valid array index. if we don't output empty table cell so fussy browsers 
      // don't mis-display table due to missing cells 
      if (isset($list[$i])) 
        print "<td><a target='_new' href='$dir$large{$list[$i]}'><img style='border-color:#000000 ' border='1' src='$dir{$list[$i]}'></a></td>"; 
      else 
        print "<td></td>"; 
         
      $imgCnt+=1; // increment images shown 
      if ($imgCnt%$perRow==0) // if image count divided by number to show per row has no remainder than it's time to wrap 
        print "</tr><tr>"; 
    } 
    print "</tr>"; 

    closedir($open); 
?> 
</table>  
0

Przede wszystkim pytanie - czy w ogóle rozumiesz ten kod?
Widzę, że jesteś początkującym programistą. Spróbuj więc przeanalizować dokładnie kod, zrozumieć wszystko i przemyśleć sobie. A następnie spróbuj zaprogramować to po swojemu (etapami, czyli np. w pierwszej wersji nie musisz robić paginacji). Jak Ci się uda, to znaczy, że rozumiesz jego działanie (prawdopodobnie). Łatwiej Ci będzie cokolwiek modyfikować.
BTW. Trochę brzydki ten kod...

0

Ten kod jest straszny.
Oddziel model (szukanie plików) od widoku (wyświetlanie tej tabeli).

1 użytkowników online, w tym zalogowanych: 0, gości: 1