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
    Regular Coder levani's Avatar
    Join Date
    Mar 2009
    Location
    Georgia
    Posts
    183
    Thanks
    29
    Thanked 1 Time in 1 Post

    Display date only for the first post

    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.

  • #2
    New Coder
    Join Date
    Aug 2003
    Location
    Derby, UK
    Posts
    97
    Thanks
    0
    Thanked 14 Times in 14 Posts
    Have a variable which holds the current "heading" (date) and each iteration of the loop check the current value against this and if different output new one and save. so (pseudocode)

    PHP Code:
    $strHeading '';
    while(
    get data from database) {
       if({
    date for this row} != $strHeading) {
          
    $strHeading = {date for this row};
          echo(
    $strHeading)
       }

    HTH,

    Dai


  •  

    Posting Permissions

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