Only show item if relevant image exists

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Only show item if relevant image exists

Post by Noobie »

Hi

I've got a MySQL db with up to 6 fields that contain paths to various images - sometimes those fields will be empty.

I'm currently showing the images like this:

Code: Select all

<? echo 
	
 '<ul id="vehicleimages">' . 
 '<li>' .  '<a href="' . $large_image1 . '">' . '<img alt="" src="' .  $small_image1 . '"  />' . '</a>' . '</li>' . 
 '<li>' .  '<a href="' . $large_image2 . '">' . '<img alt="" src="' .  $small_image2 . '"  />' . '</a>' . '</li>' . 
 '<li>' .  '<a href="' . $large_image3 . '">' . '<img alt="" src="' .  $small_image3 . '"  />' . '</a>' . '</li>' . 
	
'</ul>' ;
But this produces empty image boxes and links when there's no appropriate image available.

I'm a bit stuck as to how to check that there's an image there first, then produce the relevant html around each link and image only if it exists, if that makes sense.

Any suggestions welcomed!

Thanks
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post by thiscatis »

Could contain some small errors but I think this should do the trick

Code: Select all

// filename is something like "PRDID.gif", e.g " 1337.gif

$image = $PRDID . ".gif";

// only display if image exists

if(file_exists($image)) {
echo '<img alt="'.$name . '"border="0" src="' .$image .' "><br>';

}

else {echo '<img alt="'.$name . '"border="0" src=noimage.gif><br>'; }
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I believe he meant something like this..

Code: Select all

<?php

echo '<ul id="vehicleimages">';

if ($small_image1 != '')
{
    echo '<li>' .  '<a href="' . $large_image1 . '">' . '<img alt="" src="' .  $small_image1 . '"  />' . '</a>' . '</li>';
} else {
    echo ''; // something else..
}

if ($small_image2 != '')
{
    echo '<li>' .  '<a href="' . $large_image2 . '">' . '<img alt="" src="' .  $small_image2 . '"  />' . '</a>' . '</li>';
} else {
    echo ''; // something else..
}

if ($small_image3 != '')
{
    echo '<li>' .  '<a href="' . $large_image3 . '">' . '<img alt="" src="' .  $small_image3 . '"  />' . '</a>' . '</li>';
} else {
    echo ''; // something else..
}
       
echo '</ul>';
?>
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post by MarK (CZ) »

Code: Select all

<? echo

$output  = "<ul id='vehicleimages'>";
for ($i = 1; $i <= 3; $i++) {
  $imgL = $GLOBALS["large_image$i"];
  $imgS = $GLOBALS["small_image$i"];
  
  if (!empty($imgL) && !empty($imgS) &&
      file_exists($imgL) && file_exists($imgS))
    $output .= "<li><a href='$imgL'><img alt='' src='$imgS' /></a></li>";
}
$output .= "</ul>";

echo $output;

?>
Using $GLOBALS here is strange but you probably can easily change it to array if you're getting the values from db.
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Post by Noobie »

Thank you all for your replies - I used astions solution and it works like a dream.

Thanks again. :D
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

Should look at loading your images into an array as your method of incremental variables will make it tedious and messy to add new images.
Post Reply