Jump to content

Date grouping in displayed list


neogemima

Recommended Posts

Hello,

 

I have a php file that displays rows of a database by different criteria.  One of the criteria displayed is the date in YYYY-MM-DD format, no big deal.  Current output:

 

2008-06-15  Line of text

2008-06-15  Line of text

2008-06-14  Line of text

 

Not very elegant with a long list.  How can I, through php, display a header for a date, say 2008-06-15 on it's own line and then display just the rows beneath it's content text.  I know how to display lines without the date in front, I just need to know a way of testing for a change in the date (which is just a field in the table) and then create a header line for that date. 

 

What I'm going for:

 

2008-06-15 //perhaps some i will add a background color and change font size

Line of text

Line of text

2008-06-14

 

Any suggestions appreciated.  Thanks.  :)

Link to comment
https://forums.phpfreaks.com/topic/157665-date-grouping-in-displayed-list/
Share on other sites

Initialize a variable to remember the last value before your loop. Then test if the remembered last value is not equal to the current value. If they are different, output the header like you want and set the remembered last value to be the current value.

 

pseudo code -

 

$last_value = ""; // initialize variable
while($row = ....){
    if($last_value != $row['date']){
        // put code here to output the header
       $last_value = $row['date'];
    }
// remainder of code
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.