jason360 Posted December 18, 2014 Share Posted December 18, 2014 Hello, I am working on this function that echo's out images from my database restricted to 10 images with 4 default images if the database is empty. <?php $query = mysql_query('SELECT image_id, image_expiry FROM images ORDER BY RAND() LIMIT 10'); $i = 0; class myCounter3 implements Countable { public function count() { static $count = 0; return ++$count; } } $counter = new myCounter3; while ($row = mysql_fetch_array($query)) { if($i % 10 === 0) { } echo '<img src="http://www.mysite.com/'.$row['image_id'].'.jpg" width="300" height="auto"/>'; $i++; } for (; $i <= 4; $i++) { echo '<img src="http://www.mysite.com/default.jpg" width="300" height="auto"/>' } ?> I need to include an image expiry function with the function above, but I am not sure how to properly include it. Here is my planned expiry function (each image as an expiry date). $today = date("Y-m-d", time()); $expiry = $row['image_expiry'] if( $today > $expiry) { ## ignore expired image } else { ## display image } Bottom line I need to exclude expired images from being echoed out in the first function. Thanks in advance. Everything I have tried hasn't worked. Link to comment https://forums.phpfreaks.com/topic/293172-echo-array-with-if-statement/ Share on other sites More sharing options...
Ch0cu3r Posted December 18, 2014 Share Posted December 18, 2014 I need to include an image expiry function with the function above, but I am not sure how to properly include it. It would be better to alter your query to only return the images that have not expired yet SELECT image_id, image_expiry FROM images WHERE image_expiry > CURDATE() ORDER BY RAND() LIMIT 10 Link to comment https://forums.phpfreaks.com/topic/293172-echo-array-with-if-statement/#findComment-1500036 Share on other sites More sharing options...
jason360 Posted December 18, 2014 Author Share Posted December 18, 2014 Thanks Ch0cu3r! That was simple. I didn't know you could use > and < in the query. Link to comment https://forums.phpfreaks.com/topic/293172-echo-array-with-if-statement/#findComment-1500037 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.