Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2006
    Thanked 0 Times in 0 Posts

    Sorting an array by field(s)

    Hi everyone, I am trying to sort an array taken from an sql query by a particular field from the array. Basically I am developing my own crude PHP forums and wish to sort the threads a user has made by date and time so latest is first. I have taken a custom sort function to sort the array before looping through and printing the results. Thing is all it seems to do is remove the first entry.

    PHP Code:
              if ( 
    $x[3] == $y[3] )
              else if ( 
    $x[3] < $y[3] )
              return -
    //echo $row['thread_name'];
    //echo $row['keyword_1'];
    //echo $row['keyword_2'];
    //echo $row['keyword_3'];
    //echo $row['category_name'];
    //echo $row['username'];
    "Thread:".$count.". <a href ='showthread.php?thread_id=".$threadid."'>".$threadname."</a> Posted By: ".$threadauthor." at ".$time." on ".$date."<br />";
    Since the sort function needs to be outside the while loop, I have stored the array '$query' in the variable '$row'. Am i performing the sort on the wrong variable? Either way, what is the syntax for a while statement when I have already stored $query in $row? I am so used to doing it all in one line I am unsure how to seperate them so it works. Also I am not sure if the sort code is adapted to sort particlar fields in the database correctly, any help with this would be awesome, thanks.

    Any help is appreciated. Thanks.
    Last edited by jimbobjeff85; 04-18-2006 at 11:26 PM.

  2. #2
    Regular Coder
    Join Date
    Jan 2006
    Preston, Lancashire, England
    Thanked 0 Times in 0 Posts
    Wouldn't it be better to pull the data from the database in the correct order. Instead of pulling the data then sorting the data then printing the data


Posting Permissions

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