Jump to content

Echo Array with IF Statement


jason360
Go to solution Solved by Ch0cu3r,

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
Share on other sites

  • Solution

 

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
Share on other sites

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.