Jump to content

Recommended Posts

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);
?>

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.

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);
?>

 

 

 

 

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.