Display date only for the first post

Discussion in 'PHP' started by levani, Feb 19, 2010.

  1. #1
    I'm creating standard php loop using the while function to display latest articles on my site and I want to display each date only once at the top of the article. Something like this:

    19 February

    • Article title 1
    • Article title 2
    • Article title 3

    18 February
    • Article title 4
    • Article title 5
    • Article title 6
    • Article title 7

    17 February
    • Article title 8
    • Article title 9

    and so on...

    All the articles that is published on 18 February should be displayed under 18 February.

    How can I do that using php? Can anyone please help?

    Thanks in advance.
     
    levani, Feb 19, 2010 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $last = ''; // This goes outside of your while loop
    while($row = mysql_fetch_assoc($result)) {
        // If date isn't the same as the last one parsed
        if($row['date'] != $last) {
            // Assign it to the holding var
            $last = $row['date'];
            // And echo it out
            echo $last;
        }
    }
    PHP:
     
    JAY6390, Feb 19, 2010 IP
  3. Om ji Kesharwani

    Om ji Kesharwani Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can also do it by using GROUP BY clause
     
    Om ji Kesharwani, Feb 21, 2010 IP