Forum Moderators: coopster

Message Too Old, No Replies

nested loop

         

hanyaz

7:13 pm on Apr 14, 2009 (gmt 0)

10+ Year Member



Hello,
I have a mysql table with three fields : id, title, date.
I want to list my articles in the foloowing way :

date 1
-article 1
-article 2
-article ...

date 2
-article 1
-article 2
-article ...

and so...

actually, i want to have as a header a given date, for each date i want to see the list of articles published on it.

I would appreciate any proposal.
Thanks
hanyaz

eelixduppy

7:49 pm on Apr 14, 2009 (gmt 0)



You shouldn't need a nested loop here. The logic is simple. When you query your database, you have to GROUP BY [dev.mysql.com] date, so that those on the same date are grouped together. Then when you grab those results into your PHP application, you have to keep track of which date it is, only printing the header -- with the date in it -- each time the date changes so that it is only printed once.

And that's it. See if you can get some code together as a starting place and we'll work on it from there.

hanyaz

7:57 pm on Apr 14, 2009 (gmt 0)

10+ Year Member



Here is a first trial, but i think i need to format the date to take into account the day only...what do you think ?
<?php
$olddate = "";
$sql = "SELECT * FROM table ORDER BY date DESC";
$res = mysql_query($sql);
while ($row = mysql_fetch_array($res))
{
//si la date diffère de la précédente on l'affiche
if ($olddate != $row['date'])
{
echo $row['date']."<br />";
}
//dans tous les cas on affiche le titre de l'enregistrement en cours
echo $row['titre']."<br />";

$olddate = $row['date'];
}

?>