Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Old Oct 4, 2006, 05:05   #1
Sarah
blonde....
silver trophy
 
Sarah's Avatar
 
Join Date: Jul 2001
Location: Berkshire, UK
Posts: 9,563
order into which files are read for $dir

I am reading a directory using $dir and putting the values into an array - now the order into which the details go into the array are wrong - I want it alphabetically - how is it best to do this?

As I am paging through I cannot do a sort afterwards as that screws up all the other pages?

Is there a way that I can re-order the array after its read the directory using correct values etc?

My code (snipped) is below

Any help on this would be great as its bugging me

Thanks
Sarah

PHP Code:

//dv(DIR_FS_CATALOG . 'images/' . $products_photo_dir);

    
$dir = dir(DIR_FS_CATALOG . '/images/' . $products_photo_dir . '/');  
//    $dir_info[] = array('id' => '', 'text' => "Main Directory");
    
while ($file = $dir->read()) {  
      if (
is_file(DIR_FS_CATALOG . '/images/' . $products_photo_dir . '/' . $file))    {// && strtoupper($file) != 'CVS' && $file != "." && $file != "..") {
//dv($file);
//        $dir_info[] = array('id' => $file . '/', 'text' => $file);
          
if(preg_match('/.*\.jpg$/', $file, $matches))    {
           if(
strpos($file, '_LRG'))    continue;
           
$files[] = $file;
//             echo '<img src="/images/'.$products_photo_dir.'/'.$file.'" align="bottom" />'."<br />\n";
//           echo $file."<br /><br />\n";
        
}
      }
    }
# COULD I DO A RESORT ON ARRAY HERE INSTEAD? HOW BEST TO DO THIS?
#echo "<pre>".print_r($files)."</pre>";
#    natsort($files);  <- IF I SORT HERE IT SCREWS UP PAGINATION LATER ON
# sort above went strange look at sort(), arsort(), asort(), ksort(),
# natsort(), natcasesort(), rsort(), usort(), array_multisort(), and uksort() on php site
#echo "<pre>".print_r($files)."</pre>";
    
$num_files = sizeof($files);
    
$this_col = 1;   
       echo
"<tr>\n";
       if(
$_GET['start'] != '' && $_GET['start'] != '0')    {
           
$start    = (int)$_GET['start'];
        
$stop     = $start;
        for(
$i=0;$i<$stop;$i++)    {
            unset(
$files[$i]);
        }
       }    
//else    $start = PHOTOS_NUMBER_PER_PAGE;
printing the array to the page gives this (again snipped):

Code:
Array ( [0] => ZA066.jpg [1] => ZA228.jpg [2] => ZA266.jpg [3] => ZA082.jpg [4] => ZA273.jpg [5] => ZA263.jpg [6] => ZA097.jpg [7] => ZA030.jpg [8] => ZA174.jpg [9] => ZA088.jpg [10] => ZA001.jpg [11] => ZA224.jpg [12] => D11.jpg [13] => ZA114.jpg [14] => ZA036.jpg [15] => ZA103.jpg [16] => ZA180.jpg [17] => B08.jpg [18] => ZA213.jpg [19] => ZA013.jpg [20] => ZA280.jpg [21] => Q06.jpg [22] => ZA243.jpg [23] => D12.jpg [24] => ZA206.jpg [25] => ZA188.jpg [26] => ZA150.jpg [27] => ZA009.jpg [28] => ZA062.jpg [29] => ZA184.jpg [30] => K09.jpg [31] => ZA234.jpg [32] => ZA119.jpg [33] => ZA029.jpg [34] => T03.jpg [35] => M08.jpg [36] => F13.jpg [37] => S11.jpg [38] => ZA102.jpg [39] => ZA008.jpg [40] => ZA141.jpg [41] => ZA170.jpg [42] => ZA252.jpg [43] => ZA028.jpg [44] => S14.jpg [45] => ZA255.jpg [46] => E12.jpg [47] => ZA081.jpg [48] => ZA192.jpg [49] => R07.jpg [50] => ZA043.jpg [51] => ZA025.jpg [52] => K13.jpg [53] => ZA279.jpg [54] => E03.jpg [55] => M14.jpg [56] => ZA189.jpg [57] => ZA106.jpg [58] => ZA035.jpg [59] => G02.jpg [60] => ZA216.jpg [61] => B05.jpg [62] => ZA135.jpg [63] => H02.jpg [64] => ZA261.jpg [65] => P07.jpg [66] => ZA133.jpg [67] => ZA236.jpg [68] => E11.jpg [69] => G14.jpg [70] => ZA117.jpg [71] => P13.jpg [72] => T06.jpg [73] => ZA169.jpg [74] => K12.jpg [75] => ZA233.jpg [76] => R13.jpg [77] => ZA014.jpg [78] => ZA050.jpg [79] => ZA052.jpg [80] => H01.jpg [81] => ZA107.jpg [82] => ZA127.jpg [83] => P10.jpg [84] => ZA220.jpg [85] => G06.jpg [86] => F02.jpg [87] => ZA011.jpg [88] => ZA026.jpg [89] => ZA161.jpg [90] => ZA041.jpg [91] => ZA182.jpg [92] => ZA095.jpg [93] => H09.jpg [94] => ZA264.jpg [95] => Q03.jpg [96] => ZA235.jpg [97] => S06.jpg [98] => C12.jpg [99] => ZA047.jpg [100] => Q09.jpg [101] => ZA075.jpg [102] => ZA165.jpg [103] => B01.jpg [104] => ZA238.jpg [105] => B02.jpg [106] => F14.jpg [107] => R08.jpg  )
as you can see not exactly alphabetcally?
Sarah is offline   Reply With Quote
 

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 23:50.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved