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
Noobie
Forum Commoner
Posts: 85 Joined: Sun May 15, 2005 11:38 am
Post
by Noobie » Mon Jul 24, 2006 7:24 am
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 » Mon Jul 24, 2006 7:32 am
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>'; }
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Mon Jul 24, 2006 7:42 am
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>';
?>
MarK (CZ)
Forum Contributor
Posts: 239 Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:
Post
by MarK (CZ) » Mon Jul 24, 2006 8:04 am
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 » Mon Jul 24, 2006 9:27 am
Thank you all for your replies - I used astions solution and it works like a dream.
Thanks again.
jamiel
Forum Contributor
Posts: 276 Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom
Post
by jamiel » Mon Jul 24, 2006 9:38 am
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.