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 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Sorting an array that is a little messed up?

    Ok... maybe this is really simple but I've spent almost all day on it, reading the PHP documentation, websites, etc. I am trying to perform a sort of some data from a mysql database, by DATE. However, the original people who made it (and the entire website) were a little dense, and stored the dates as varchar(15) in the format ddmmyy.

    I need to retrieve multiple attributes, but sort only by one of the them: con_date.

    I have a if(sortby==relevance) ... elseif(sortby==age) condition. The relevance sort isn't a problem. It loops through the array and echos out (in relevance order) a headline, text, and the date in a readable format using a function that converts it:
    Code:
    function convertDate($date)
    {
    	$date = (substr($date, 4, 2))."-".(substr($date, 2, 2))."-".(substr($date, 0, 2));
    	$timestamp = strtotime("$date");
    	$date = date('F j, Y', $timestamp);
    	return $date;
    }
    ... then where required:
    Code:
    $date = $line[con_date];
    $newdate = convertDate($date);
    echo $newdate;
    That might be useful to solve the problem of the date sort but personally I don't know how just yet.

    Incase anyone is wondering what the SQL query looks like, it's this:
    Code:
    $query =  "SELECT con_id, con_category, con_shorthead, con_shorttext, con_text, con_date, MATCH ";
    $query .= "(con_text) AGAINST ('$search_submission') AS score FROM apr_content WHERE MATCH (con_text) AGAINST ('$search_submission') "; 
    $query .= "LIMIT $limitvalue, $page_item_limit";
    $result = mysql_query($query) or die(mysql_error());
    And the elseif condition I'm stuck on:
    Code:
    elseif($sortby=="age")
    {
    	// print out results in a table	
    	$line = mysql_fetch_array($result, MYSQL_ASSOC);
    	..........?
    Helpppppppppppppppp! lol

    I was thinking some implementation of asort, but I'm quite new to array stuff really and I can't quite get anything to work.

    Loop through the array converting con_date into an accurate sortable date (yymmdd maybe)..... and then sort all the results by con_date, and echo out? Would it be right to say I needed to "sort a multimensional array by value"? :S

    Can't be that hard, surely??

    Really, really super thanks for any tips / pointers / help
    Last edited by neovo; 07-07-2007 at 10:49 PM.


 

Posting Permissions

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