tags:

views:

79

answers:

4

I have a mysql database of entries with dates. So what I want is to show all the dates in my database and then under each date, I want to show all the entries in the database entered on the specefic date. I am thinking of two loops but I don't know how to write the condition to display all the dates in my database before I loop out the entries under that date.

Edit: i used NOW() to store the date. Using the loop by longneck, how can ignore the time when checking if date is sameas previous? Should I explode? What's the best way?

+1  A: 

I'm assuming that you want a list of the dates so that you can then do separate queries for each date's entries. Usually you would use a query like:

SELECT DISTINCT date_field FROM table_name ORDER BY date_field DESC

(this will do it newest-first, remove DESC to make it oldest-first)

Now, you probably don't want to do it this way, because it will require a lot of queries (one for each day's entries). It is more efficient to just order the entries by the date, and then use logic in your php to print out the headers. So you would use code like:

$result = $db->query("SELECT * FROM table_name ORDER BY date_field DESC");
$current_date = "";
while ($row = $result->fetch_array()) { 
   if ($current_date != $row["date_field"]) { 
       echo "<h3>{$row['date_field']}</h3>";
       $current_date = $row["date_field"]; 
   } 
   // Print your entry from $row here. 
}
jamuraa
A: 

you would probably want to get that in your SQL statement: (assuming it is datetime type)

SELECT * FROM table WHERE date BETWEEN '2009-9-8 00:00:00' AND '2008-9-8 23:59:59'

Then just do your normal loop through your results

kilrizzy
Sorry, I misunderstood the question at first. You could probably loop through all the dates and list them using the DISTINCT method from jamuraa, then inside that loop that lists them have this one run.
kilrizzy
+4  A: 

you should use one query, sort it by date, and detect the change in the date as a signal to display the next date:

<?php

$sql = 'select start_date, name from events order by start_date';

$res = mysql_query($sql) or die(mysql_error());

$prev_date = null;

while ($row = mysql_fetch_assoc($res)) {
  if ($row['start_date'] != $prev_date) {
    echo "<h1>{$row['start_date']}</h1>"\n;
    $prev_date = $row['start_Date'];
  }

  echo "<p>{$row['name']}</p>";
}

?>
longneck
+1 This is what I often do too. BTW, PHP array keys are case-sensitive, but you use 'start_Date' in one case above.
Bill Karwin
What is Name for in "start_date, name"?
Doug
Wait, this current only shows the date as h1 and then only 1 entry beneath that date. I want it to show all the entries for that date. E.g.January 6, 2007entry 1, entry 2, entry 3
Doug
Oh I know why... I user now() to insert time and date, so that's why the loop is now wrking the way I want. Is there a way to just have it check with date?
Doug
Let me rewrite what I was saying. I used NOW() to insert date and time for mysql, so it is in this format 2099-7-23 23:35:54. Is there anyway to ignore time ? Maybe an explode?
Doug
@doug *What is Name for in "start_date, name"?*: you didn't provide a description of your table so i had to make something up.
longneck
@doug *Is there a way to just have it check with date?*: see http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date
longneck
A: 

Also have a look at the GROUP BY clause of the SELECT statement.

Loadmaster