Jump to content

Echo Array with IF Statement


jason360

Recommended Posts

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

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.