View Full Version : Categorizing posts by date
Alex Taylor
11-23-2003, 05:51 AM
I'm trying to write a journal-type script.. The main page displays all the journal entries, organized by descending date. I want to make it so that if a user has, say, 3 posts on the same date, it will automatically organize all the posts from that day under one header. Example:
Post 1 - Jan 1st
Post 2 - Jan 1st
Post 3 - Jan 1st
Post 4 - Jan 4th
Instead of looking like that, I want it to look like this:
Jan 1st
-Post 1
-Post 2
-Post 3
Jan 4th
-Post 4
Any ideas on how I would accomplish this? Any tips are appreciated! I've played around a tiny bit with the GROUP BY control when I'm pulling data out of the dB but I can only seem to get it to display the first rows with each different date.
assassingod
11-23-2003, 08:08 AM
I've had a quick try (and seeing as i'm not sure how your table is setup) and i've got a little way but sadly it's not the most efficient way - but i hope you can work around it (or someone posts an easier way to do it)
<?php
//connect to the DB
$conn = mysql_connect("localhost","root","")
or die("Couldnt connect"); //failed?
$rs = mysql_select_db("postdate", $conn)
or die("Couldnt select DB!"); // failed?
//run query
$querytime = mysql_query("SELECT * FROM post WHERE postdate = '1st Jan'");
// loop results
while($posted = mysql_fetch_array($querytime))
{
$poststuff .= "-$posted[post]<br />";
}
//run query again
$querytime1 = mysql_query("SELECT * FROM post WHERE postdate = '4th Jan'");
// loop results again
while($posted1 = mysql_fetch_array($querytime1))
{
$poststuff2 .= "-$posted1[post]<br />";
}
//echo crappy results, i had to hardcode in the dates sadly
echo("1st Jan <br />$poststuff<br />4th Jan<br /> $poststuff2 ");
?>
Alex Taylor
11-23-2003, 03:03 PM
Thanks, but unfortunately I can't hardcode the dates. The GROUP BY function works in that it takes each different date and displays the first row of each, but it just doesn't continue and display all the other similar dates under one another.
Alex Taylor
11-25-2003, 12:54 AM
Anybody? Beuler?
Issvar
11-25-2003, 09:55 AM
Use a query like "SELECT post,date FROM posts ORDER BY date", and loop through it, like
$curdate=""
while ($data = mysql_fetch_object($queryresults)) {
if ($data->date != $curdate) {
echo '<br>'.$data->date.'<br>';
$curdate = $data->date;
}
echo $data->post.'<br>';
}
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.