Forum Moderators: coopster

Message Too Old, No Replies

mysql selecting fields by date

need to check whether a date is expired or not

         

generic

6:00 pm on Feb 27, 2009 (gmt 0)

10+ Year Member



I'm trying to pull off a 'featured sale item' type of deal, and I'm having trouble selecting a single (random) entry based on whether or not the item is 'expired' based on todays date. The following will output entries, but if the entry is expired, it just shows an empty div.

mysql
-----
title VARCHAR(40),
details VARCHAR(9999),
oldprice VARCHAR(30),
newprice VARCHAR(30),
expires VARCHAR(30), //unix time stamp
expiretext VARCHAR(30), // formatted output of 'expires'
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id))")

php
---
// grab info from db
$query = "SELECT * FROM featuredsales ORDER BY RAND() LIMIT 0,1 ";
$result = mysql_query($query);

// start html for looped data
echo '<div class="sales_display">';

// begin loop from db
while($row = mysql_fetch_array($result))
{

// compare dates for expired items
if(strtotime($row['expiretext']) > strtotime($today)){

// get price difference for amount saved
$savings = $row['oldprice'] - $row['newprice'];
$savings_percentage = round( ($savings/$row['oldprice'])*100, 0 );

// make date more readable
$sale_expiry = date('F jS, Y', strtotime($row['expiretext']));

// display active items
echo '<div id="sale_'.$row['id'].'">
<h3>'.$row['title'].'</h3>
<p><strong>Now: &#36;'.$row['newprice'].'</strong><br />
Was: &#36;'.$row['oldprice'].'<br />
You Save: &#36;'.$savings.' ('.$savings_percentage.'&#37;)</p>
<div class="mce_output" id="sale_'.$row['id'].'_details">'.$row['details'].'</div>
<p>(<em>this sale expires on '.$sale_expiry.'</em>)</p>
</div>
<hr />';

// don't display expired items
} else {
echo ''; }

// end sales loop
}

// end html for looped data
echo '</div>';

mysql_close($conn);

[edited by: eelixduppy at 8:36 pm (utc) on Feb. 28, 2009]
[edit reason] disabled smileys [/edit]

sonjay

6:23 pm on Feb 27, 2009 (gmt 0)

10+ Year Member



Why don't you use your query to select only an item that is not expired? Then only output the div (and its contents) if mysql_num_rows < 0.

LifeinAsia

6:26 pm on Feb 27, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



You'll need to add the expiry checking to your SELECT statement. Use something like:
SELECT *
FROM featuredsales
WHERE expires > [TODAY's DATE]
ORDER BY RAND() LIMIT 0,1

I don't do PHP/MySQL, so I don't know the exact syntax you'll need for [TODAY's DATE].

generic

7:22 pm on Feb 27, 2009 (gmt 0)

10+ Year Member



Ok, that makes sense. I tried the following:


$today = time();
$query = "SELECT * FROM featuredsales WHERE expires > $today ORDER BY RAND() LIMIT 0,1 ";

'$today' returns the current timestamp, but I can't seem to get it working in the select. Either nothing displays or I get the following error:


Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in . . .

Am I using the wrong syntax in the query?

generic

7:34 pm on Feb 27, 2009 (gmt 0)

10+ Year Member



I got it, $today was being used elsewhere in the script. All seems well for the moment. Thanks for the help folks!