Obsolete80 Posted May 15, 2010 Share Posted May 15, 2010 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... Link to comment https://forums.phpfreaks.com/topic/201868-replacing-missing-image/ Share on other sites More sharing options...
kenrbnsn Posted May 15, 2010 Share Posted May 15, 2010 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 Link to comment https://forums.phpfreaks.com/topic/201868-replacing-missing-image/#findComment-1058760 Share on other sites More sharing options...
Obsolete80 Posted May 15, 2010 Author Share Posted May 15, 2010 Thanks for the help, I tried a few ways of checking that an image existed, but just couldn't figure it out! Link to comment https://forums.phpfreaks.com/topic/201868-replacing-missing-image/#findComment-1058763 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.