PDA

View Full Version : Displaying Date Once Above News


Jedi Josh
08-19-2003, 11:46 AM
Ok so I'm working on a really simple news script, and I'm having a little problem. What I want is to have the date displayed once above the news that was submitted that day (exactly the way Massassi's is posted), but I can't figure out exactly how to get it done...

Anyone help? http://forums.massassi.net/html/smile.gif

------------------
To think that once I could not see beyond the veil of reality, to see those who dwell behind. I was once a fool.

Aaron
08-19-2003, 02:47 PM
What you could do is to have a variable, sDate, which stores the last date of the item displayed, then compair sDate to the date of the news item that was returned from the database, if they aren't the same, then you can write the date header and then the news item. Just initalize sDate to something that isn't a date and it should work well. I hope I explained that well enough.

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

Jedi Josh
08-19-2003, 02:53 PM
I'll try my best http://forums.massassi.net/html/smile.gif.

------------------
To think that once I could not see beyond the veil of reality, to see those who dwell behind. I was once a fool.

Jedi Josh
08-19-2003, 04:16 PM
Grrrrrr!

I'm gonna kill someone! I just can't figure this out!

------------------
To think that once I could not see beyond the veil of reality, to see those who dwell behind. I was once a fool.

Aaron
08-19-2003, 04:33 PM
Alright, in puesdo code:


sDate="";

news=database.query("SELECT NewsDate,NewsSubject,NewsText FROM News ORDER BY -NewsID");

while(!news.eof)
{
if(news[0]!=sDate)
{
sDate=news[0];
printf("News for %s<br>\n",sDate);
}

printf("Subject: %s<br>Content: %s<br><br>",news[1],news[2]);
}



If that doesn't help, you could post what you've come up with.

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

Jedi Josh
08-19-2003, 05:03 PM
I think I got it guys. Here's what I finally managed to come up with, with the help of the guys at the DevShed forums.

<?php
mysql_connect('localhost','user','pass');
mysql_select_db('tourn');

$query = mysql_query('SELECT * FROM mytable ORDER BY date DESC');

while($result=mysql_fetch_assoc($query)) {
if($prevD != $result['date']) {

echo("<center>".$result['date']."</center>");
echo("<br>");
$prevD=$result['date'];

}

echo("<h2>".$result['title']."</h2>");
echo($result['news']);

}
?>


It works fine on my machine. Sure I need to improve it alot, but it does the function I wanted http://forums.massassi.net/html/smile.gif.

Anything potentially wrong with the above code?

------------------
To think that once I could not see beyond the veil of reality, to see those who dwell behind. I was once a fool.

Jedi Josh
08-19-2003, 09:11 PM
Ok the code works fine, but I'm having a little trouble understanding exactly how it works. Just the loop really. Anyone explain it to stupid ole Jedi Josh? http://forums.massassi.net/html/biggrin.gif

------------------
To think that once I could not see beyond the veil of reality, to see those who dwell behind. I was once a fool.