Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    1
    Thanks
    0
    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:
    $row=mysql_fetch_array($query);
          function 
    compare($x$y)
              {
              if ( 
    $x[3] == $y[3] )
              return 
    0;
              else if ( 
    $x[3] < $y[3] )
              return -
    1;
              else
              return 
    1;
              }
          
    usort($row'compare');
          while(
    $query==true)      
            {
            
    $date=$row['date'];
            
    $time=$row['time'];
            
    $threadid=$row['thread_id'];
            
    $threadname=$row['thread_name'];
            
    //echo $row['thread_name'];
            
    $kw1=@$row['keyword_1'];
            
    //echo $row['keyword_1'];
            
    $kw2=@$row['keyword_2'];
            
    //echo $row['keyword_2'];
            
    $kw3=@$row['keyword_3'];
            
    //echo $row['keyword_3'];
            
    $catname=$row['category_name'];
            
    //echo $row['category_name'];
            
    $threadauthor=$row['username'];
            
    //echo $row['username'];
      
            
    $count++;
     
            echo 
    "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
    Regular Coder
    Join Date
    Jan 2006
    Location
    Preston, Lancashire, England
    Posts
    285
    Thanks
    0
    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
    •