SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Internet Consumist Kayn's Avatar
    Join Date
    Feb 2001
    Location
    Missouri
    Posts
    209
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Sort by date via arrays.

    Hey hey!

    I'm having a bit of an issue sorting by date with arrays. For example...

    I did some scouring around and found these dateAsc and dateDesc functions. It doesn't work at all.

    PHP Code:
    function dateAsc($a$b) {
        return 
    strcmp($a["date"], $b["date"]);
    }

    function 
    dateDesc($a$b) {
        return 
    strcmp($b["date"],$a["date"]);
    }

    while (
    false !== ($file readdir($dir_handle))) {  //loop through all the files in the path
            
    if ($file == "." || $file == "..") {
                continue;
            }  
    //ignore these
            
            // these have to be htm files
            
    if (substr_count($file".htm") > 0) {
                
    // now we gotta make the files ledgible. 
                
    $arr_split_it explode("__",$file);
                
    $press_date[$count] = strtotime($arr_split_it[0]);
                
    $press_title[$count] = $arr_split_it[1];
                
                
    $press_list[$count] = "<li><a href=\"press.php?file=" urlencode($file) . "\">" str_replace("__"," ",str_replace(".htm","",stripslashes(urldecode($file)))) . "</a></li>";
                
    $count++;
            }
        }

    uasort($press_date"dateDesc"); 
    The output looks like this:


    • 07-01-08 New Power Engineer
    • 04-08-08 Data Network
    • 04-08-09 Fiber to the Home
    As you can see, the dates are not in order.

    Basically this scripts reads files from a directory with the date in the file name, removes the double underscore and the .htm extension and displays it as a readable format on a web page.

    All I need to be able to do now is sort them by the date descending. And no, databases are not an option here.

    Any thoughts? Thanks!
    Sing in a band called Psychostick, Alfredo Afro.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Storing and sorting the date, title and list in separate arrays won't work. Even if you sort the date array correctly, now the order of its values doesn't match the order of the values in the other arrays, and it becomes useless information.

    Keep all the file information in a multidimensional array and sort that whole array by date.

  3. #3
    Internet Consumist Kayn's Avatar
    Join Date
    Feb 2001
    Location
    Missouri
    Posts
    209
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm. That's interesting. I'm not very familiar with multi-dimensional arrays.

    Using my example above, how would you take that file information and place it into a multidimensional array?
    Sing in a band called Psychostick, Alfredo Afro.

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    In addition to your current arrays:

    PHP Code:
    $data[$count]['date'] = strtotime($arr_split_it[0]);
    $data[$count]['title'] = $arr_split_it[1];
    $data[$count]['list'] = "<li><a href=\"press.php?file=" urlencode($file) . "\">" str_replace("__"," ",str_replace(".htm","",stripslashes(urldecode($file)))) . "</a></li>"
    Try try to do something with array_multisort using the date and the $data arrays, maybe.

  5. #5
    Internet Consumist Kayn's Avatar
    Join Date
    Feb 2001
    Location
    Missouri
    Posts
    209
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool I'll give it a try. Thanks!
    Sing in a band called Psychostick, Alfredo Afro.

  6. #6
    Internet Consumist Kayn's Avatar
    Join Date
    Feb 2001
    Location
    Missouri
    Posts
    209
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, it does work after converting the dates to unix timestamps and sorting them that way.

    Thanks Dan!
    Sing in a band called Psychostick, Alfredo Afro.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •