Jump to content

Don't display image if it's not in the folder


bruisr

Recommended Posts

Hey guys - I have some code that pulls an image out of a folder and displays it on the page. The problem is, there won't always be an image to display in which case I'd rather the code not even display.

 

Here's my code so far:

<tr><td><a href="images/uploads/<?php echo $row['loc_id']; ?>_1.jpg"><img id="morephoto" src="images/uploads/thumbs/<?php echo $row['loc_id']; ?>_1thb.jpg" /></a></td></tr>

 

The images are renamed on upload to have the id number for that row appended to the front of the file name and that is how I'm calling them back in.

 

I know I need to write an if statement that contains the code from the <tr> to the </tr> to display if the image exists, the problem is, since there isn't a field for this in the database, I don't know how to check it? I'm still a noob so I appreciate any help that is offered.

 

Thanks!

Link to comment
Share on other sites

I use something like if ($row['loc_id']) {echo "$row['loc_id']";}

 

In some browsers td collapses so I'd test and echo a space   if that's the case

 

Thanks for the reply. Um, could you be a bit more specific? Explain it to me like you would to a 4 year old learning why 1 + 1 = 2  ;D

Link to comment
Share on other sites

I'm assuming your in a while loop like

 

 

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)){

echo'

<tr><td>'; if ($row['loc_id']) {echo "<a href="images/uploads/$row['loc_id']\"" ><img src="images/uploads/thumbs/ $row['loc_id'];} else {echo ' ';} echo'</td></tr>';

}

 

You'll have to play with it, your errors will tell you whats wrong. I used to use the concatenation stuff and still do if it's just ''.$var.'' but sometimes that gets confusing if your using if's

Link to comment
Share on other sites

I'm actually not using a loop. Because there are multiple files to be called that have a different file name, I figured it would be best code it separately? Either way, it's what I did :P

 

Though your post didn't fix it exactly, it did prompt me to do another google search which led me to the 'file_exists' function. This now works for me:

 

<?php
if (file_exists('images/uploads/' . $row['loc_id'] . '_1.jpg')) {
echo '<tr><td><a href="images/uploads/' . $row['loc_id'] . '_1.jpg"><img id="morephoto" src="images/uploads/thumbs/' . $row['loc_id'] . '_1thb.jpg" /></a></td></tr>';
}
?>

 

I just copy that and change it for the _2, _3 and _4 that I have for my 4 images. Tested it on a page with pics and one with out and it works great.

 

Thanks for your help! What part of Florida are you in? I'm in the Leesburg area.

Link to comment
Share on other sites

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.