craigeves Posted March 19, 2010 Share Posted March 19, 2010 I have a table called 'promotions' with the rows 'promoID', 'promo', 'description' and 'expiry'. I don't want the page to show a promotion if it has expired, but can't quite get it to work. This is what I have. Can anyone tell me where i'm going wrong? Thanks in advance <?php require_once('Connections/hairstation.php'); mysql_select_db($database_hairstation, $hairstation); $query_promotions = "SELECT * FROM promotions"; $promotions = mysql_query($query_promotions, $hairstation) or die(mysql_error()); $row_promotions = mysql_fetch_assoc($promotions); $totalRows_promotions = mysql_num_rows($promotions); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Show Promotions</title> </head> <body> <table border="0" cellpadding="5"> <tr> <td>promo</td> <td>description</td> <td>expiry</td> </tr> <?php $today = date("F j Y"); ?> <?php do { ?> <?php if ($row_promotions['expiry'] < '$today') { ?> <tr> <td><?php echo $row_promotions['promo']; ?></td> <td><?php echo $row_promotions['description']; ?></td> <td><?php echo date("F j Y ", strtotime ($row_promotions['expiry'])) ?></td> </tr> <?php } ?> <?php } while ($row_promotions = mysql_fetch_assoc($promotions)); ?> </table> </body> </html> <?php mysql_free_result($promotions); ?> Quote Link to comment https://forums.phpfreaks.com/topic/195787-if-date-is-before-current-date-dont-show/ Share on other sites More sharing options...
Wolphie Posted March 19, 2010 Share Posted March 19, 2010 You can't really make it work like that, you would have to convert the data in the database to a time integer and then compare it with time() http://php.net/manual/en/function.time.php Quote Link to comment https://forums.phpfreaks.com/topic/195787-if-date-is-before-current-date-dont-show/#findComment-1028509 Share on other sites More sharing options...
PFMaBiSmAd Posted March 19, 2010 Share Posted March 19, 2010 One of the great points of using a database is you only retrieve the data you are interested in. Your query should contain a WHERE clause that only retrieves rows that have expire dates that are greater than or equal to the current date. Let the database engine do the work for you instead of writing a lot of extra code (php is a slow parsed, tokenized, interpreted language compared to the compiled code that the database engine uses) to scan through the results of the query. However, given the date format string you are using 'F j Y', the dates are not stored in a format that permits direct greater-than/less-than comparisons or ordering. Your first step would be to start storing the dates in your table using a DATE data type, that what it exists for. Quote Link to comment https://forums.phpfreaks.com/topic/195787-if-date-is-before-current-date-dont-show/#findComment-1028510 Share on other sites More sharing options...
craigeves Posted March 19, 2010 Author Share Posted March 19, 2010 Your first step would be to start storing the dates in your table using a DATE data type, that what it exists for. I'm already doing this - I'm just not sure on the next step. I'm new to PHP and am relying on Dreamweaver widgets to help me code initially, but then I disect what has been written and re-write it. I'm just stuck on getting this to work. I had another crack - see below but still nothing. I'm not sure I understand how to do this. <?php require_once('Connections/hairstation.php'); mysql_select_db($database_hairstation, $hairstation); $query_promotions = "SELECT * FROM promotions"; $promotions = mysql_query($query_promotions, $hairstation) or die(mysql_error()); $row_promotions = mysql_fetch_assoc($promotions); $totalRows_promotions = mysql_num_rows($promotions); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Show Promotions</title> </head> <body> <table border="0" cellpadding="5"> <tr> <td>promo</td> <td>description</td> <td>expiry</td> </tr> <?php $future = strtotime($row_promotions['expiry']); $now = time(); ?> <?php do { ?> <tr> <?php if ($future < $now){ ?> <td><?php echo $row_promotions['promo']; ?></td> <td><?php echo $row_promotions['description']; ?></td> <td><?php echo date("F j Y ", strtotime ($row_promotions['expiry'])) ?></td> <?php } ?> </tr> <?php } while ($row_promotions = mysql_fetch_assoc($promotions)); ?> </table> </body> </html> <?php mysql_free_result($promotions); ?> Quote Link to comment https://forums.phpfreaks.com/topic/195787-if-date-is-before-current-date-dont-show/#findComment-1028530 Share on other sites More sharing options...
PFMaBiSmAd Posted March 19, 2010 Share Posted March 19, 2010 The following query will select only the rows that have an expiry date greater-than or equal to the current date and it will order the rows with ascending expiry dates - $query_promotions = "SELECT * FROM promotions WHERE expiry >= CURDATE() ORDER BY expiry"; You then just need to retrieve and display the rows. You should not use a do-while loop as it requires more logic to accomplish the same results as just using a simpler while(){} loop. Quote Link to comment https://forums.phpfreaks.com/topic/195787-if-date-is-before-current-date-dont-show/#findComment-1028532 Share on other sites More sharing options...
craigeves Posted March 19, 2010 Author Share Posted March 19, 2010 Thank you so much - this does the trick and it's so much simpler to understand now. The following query will select only the rows that have an expiry date greater-than or equal to the current date and it will order the rows with ascending expiry dates - $query_promotions = "SELECT * FROM promotions WHERE expiry >= CURDATE() ORDER BY expiry"; You then just need to retrieve and display the rows. You should not use a do-while loop as it requires more logic to accomplish the same results as just using a simpler while(){} loop. Quote Link to comment https://forums.phpfreaks.com/topic/195787-if-date-is-before-current-date-dont-show/#findComment-1028548 Share on other sites More sharing options...
PFMaBiSmAd Posted March 19, 2010 Share Posted March 19, 2010 It's almost always simpler and faster to let the database engine retrieve the rows you are interested in, in the order that you want them, and with each piece of data formatted the way you want it. Quote Link to comment https://forums.phpfreaks.com/topic/195787-if-date-is-before-current-date-dont-show/#findComment-1028553 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.