Jump to content

[SOLVED] displaying by date and grouping by day


scarhand

Recommended Posts

im trying to display results from my database by date and group them together by day.

 

this is what the mysql table looks like:

 

title | date

test title 4 | 2008-01-11 05:14:56

test title 3 | 2008-01-11 05:14:56

test title 2 | 2008-01-10 05:14:56

test title 1 | 2008-01-10 05:14:56

 

and i want the results to look like this:

 

january 11, 2008

 

test title 4

test title 3

 

january 10, 2008

 

test title 2

test title 1

 

i have been messing around with this for a while and cant get it to work the way i want it to. you can probably do this in 1 mysql statement but heres the coding i have so far:

 

<?php

$sql1 = mysql_query("SELECT * FROM videos GROUP BY date");
$sql2 = mysql_query("SELECT * FROM videos ORDER BY date DESC");
$count = mysql_num_rows($sql1);

if ($count == 0)
{
  echo 'There are no videos to display';
}
else
{
  while ($row = mysql_fetch_array($sql1))
  {
    $date = date("M j, Y", strtotime($row['date']));
  
    echo "$date<br><br>";
    
    while ($row = mysql_fetch_array($sql2))
    {
      $title = $row['title'];

      echo "$title<br>";
    }
  }
}

?>

 

any help is greatly appreciated

Link to comment
Share on other sites

Use one query that uses the "ORDER BY". You can also use DATE_FORMAT() to return the date the way you want. Return one format for displaying and return another in YYYYMMDD format so it can be used to compare.

 

Then as you read each row have PHP logic where you check when the date has changed. Each time the date changes, you display the title and keep track of the new date so you can compare it to the next row (to see if it's changed).

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.