Page 1 of 1

Only show item if relevant image exists

Posted: Mon Jul 24, 2006 7:24 am
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

Posted: Mon Jul 24, 2006 7:32 am
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>'; }

Posted: Mon Jul 24, 2006 7:42 am
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>';
?>

Posted: Mon Jul 24, 2006 8:04 am
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.

Posted: Mon Jul 24, 2006 9:27 am
by Noobie
Thank you all for your replies - I used astions solution and it works like a dream.

Thanks again. :D

Posted: Mon Jul 24, 2006 9:38 am
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.