Jump to content

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!

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

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

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.

I'm actually not using a loop. ... google search which led me to the 'file_exists' function. This now works for me:

 

Okay, that looks like a better way to go.

 

Anyway, I'm in North East Florida near Jacksonille.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.