Jump to content

[SOLVED] PHP + MYSQL - Display Results Grouped by Date


hutch

Recommended Posts

Can any one tell me how to take data extracted from mySQL and make it display results that have the same date grouped together?

 

Typically I used a "while ($row = mysql_fetch_array($data))" to display data but can't figure out the easiest way to do this.

 

 

Example:

 

Sept. 10 2008

-----------------------

row1

row4

 

Sept. 11 2008

-----------------------

row2

row3

 

Sept. 18 2008

-----------------------

row5

row6

row7

row8

etc..

Hi,

 

Pull your results sorting by date order so adding a

order by <date_field> DESC

to your query if you dont have it already.

 

Then you can compare the last date row against the current if its the same just print the row if its different print the header first.

 

Example:

 

$sql = "SELECT * from TABLE order by <date_field> DESC"
$old_date = False;

$result = mysql_query($sql);
while ($row = dbarray($result))
{
   if ($old_date != $row['date_field'])
   {
      echo $row['date_field']."<BR />";
   }

   echo $row."<BR />";
   $old_date = $row['date_field']; 
}

 

Not Tested but should work.

 

Dave

One more stupid question.. My date field in mySQL is setup exactly as that, a "date" column.

 

When my results are being output it shows as:

 

2008-09-10

..

2008-09-11

..

2008-09-12

..

2008-09-21

..

2008-09-31

..

2008-09-01

 

The lower dates (01, 02, 03, etc) are being displayed at the bottom.. What have I done wrong?

 

mySQL

SELECT * FROM table ORDER BY startDate

 

PHP

while ($row = mysql_fetch_array($result)) {
if ($old_date != $row['StartDate']) {
  echo $row['StartDate'].'<br />';
}

echo $row['Title'].'<br />';
$old_date = $row['StartDate']; 
}

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.