Jump to content

Replacing missing image


Obsolete80

Recommended Posts

I'm attempting to replace missing images from a site that I'm currently working on with a standard image (images/unavailable.jpg) until I have the relevant images. The only way I've been able to achieve this so far is to have copies of unavailable.jpg saved with the image names that are missing, which means I currently have about 20-30 copies of the same image.

 

Is there a simple way to do this?

 

 

The code I currently have is displayed below:

$result = mysql_query("SELECT * FROM pigs WHERE status LIKE 'available'");
$num_rows = mysql_num_rows($result);

echo " We currently have <strong> $num_rows </strong> guinea pigs available for rehoming<br/><br/><br/>"; 

//build query

$query = mysql_query("SELECT * FROM pigs WHERE status LIKE 'available' ORDER BY added");

//display results

while ($row = mysql_fetch_array($query)) {
echo "<img src='images/pigs/".$row['id'].".jpg' alt='' border='0' class='thumb' />".
" - <a href='details.php?id=".$row['id']."'>".$row['name']."</a> - ".$row['sex']." , ".$row['age']." months<br/><br/><br/>"
	;}

 

 

I'm new to php so theres probably a really simple solution to this, but I've been unable to find it so far...  :confused:

Link to comment
https://forums.phpfreaks.com/topic/201868-replacing-missing-image/
Share on other sites

Test to see if the image exists, if not display the "unavailable image" instead, so replace

<?php
echo "<img src='images/pigs/".$row['id'].".jpg' alt='' border='0' class='thumb' />".
" - <a href='details.php?id=".$row['id']."'>".$row['name']."</a> - ".$row['sex']." , ".$row['age']." months<br/><br/><br/>";
?>

with

<?php
      $img = (file_exists('images/pigs/' . $row['id'] . '.jpg'))?'images/pigs/' . $row['id'] . '.jpg':'images/unavailable.jpg';
echo "<img src='$img' alt='' border='0' class='thumb' />".
" - <a href='details.php?id=".$row['id']."'>".$row['name']."</a> - ".$row['sex']." , ".$row['age']." months<br/><br/><br/>";
?>

 

For this to work, you have to delete all of the extra images.

 

Ken

 

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.