Comparing dates from Mysql and PHP
Hi, I am a PHP beginner, I am working this code and I need help, And kinda stack of it.
I have a database with 5 fields(id, date, time , title and description). This is what I want to do; I want my script to go into the database fetch the date and compares dates if both days are equal, it should echo or read the dates as one and the titles and descriptions will be display with one dates that corresponds to the date taken from Mysql but when the dates are not equal it will display another dates. Your help is really highly appreciated guys.. Thanks in advance. Example : If dates are similar it will display in one category of dates November 9, 2010 Updates Sample 1 This is a description. Sample 2 This is a description. Sample 3 This is a description. But if the dates is not equal it will display another date category November 8, 2010 Updates Sample 1 This is a description. Sample 2 This is a description. Any help please? What should i do after this codes? <?php $result = mysql_query("SELECT * FROM tbl_localnews"); while($rows = mysql_fetch_array($result) ){ //If the dates in database is exist in many times. //Display it in one date category. if ( $rows > 1 ) { ?> <h3 class="update"> <em>News for <?php print $rows['date']; ?></em><em style="font-size:12px; float:right;">Updated <?php echo $rows['time'] ?></em></h3> <?php // If Not the same Display dates in another category. }else{?> <h3 class="update"> <em>News for <?php print $rows['date']; ?></em><em style="font-size:12px; float:right;">Updated <?php echo $rows['time'] ?></em></h3> <?php } } ?> But my codes does not work. It display all the dates even if it is similar dates. Can any one help me please..
2 Answers
I suggest you use the database to group the news items you want, such as:
SELECT IF(date = <date variable>, 'equal', 'not equal') as date_equal, id, title, ... FROM tbl_localnews;However, you rarely want to use eqality when looking at dates, you probably want a time period, so that you can differentiate between old and recent news: SELECT IF(date >= <date variable>, 'recent', 'old') as recent_news, id, title, ... FROM tbl_localnews;Or you could simply use two seperate queries: $recent_results = mysql_query("SELECT * FROM tbl_localnews WHERE date > <date variable>"); $old_results = mysql_query("SELECT * FROM tbl_localnews WHERE date < <date variable>"); Posted: Go 1 of 1 people found this answer helpful. Did you? Yes No
Firstly, what is the type of your date field? DATE / DATETIME / INT ? I'm going to assume date for now.
$last_date = ""; $result = mysql_query("SELECT * FROM tbl_localnews ORDER BY date, time"); while ($row = mysql_fetch_array($result)) { if ($row['date'] != $last_date) { print("<h2>News for ".$row['date']."</h2>"); $last_date = $row['date']; } print("<h3>".$row['title']."</h3>"); print("<p>".$row['description']."</p>"); } mysql_free_result($result);You should be able to modify the above example to suit your needs... Posted: MacOS 3 of 3 people found this answer helpful. Did you? Yes No Thanks Jooria. It really works. Your the best bro. I salute on you. |
© Advanced Web Core. All rights reserved