bruisr Posted November 3, 2011 Share Posted November 3, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/250385-dont-display-image-if-its-not-in-the-folder/ Share on other sites More sharing options...
floridaflatlander Posted November 3, 2011 Share Posted November 3, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/250385-dont-display-image-if-its-not-in-the-folder/#findComment-1284699 Share on other sites More sharing options...
bruisr Posted November 3, 2011 Author Share Posted November 3, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/250385-dont-display-image-if-its-not-in-the-folder/#findComment-1284702 Share on other sites More sharing options...
floridaflatlander Posted November 3, 2011 Share Posted November 3, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/250385-dont-display-image-if-its-not-in-the-folder/#findComment-1284709 Share on other sites More sharing options...
bruisr Posted November 3, 2011 Author Share Posted November 3, 2011 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 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. Quote Link to comment https://forums.phpfreaks.com/topic/250385-dont-display-image-if-its-not-in-the-folder/#findComment-1284714 Share on other sites More sharing options...
floridaflatlander Posted November 3, 2011 Share Posted November 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/250385-dont-display-image-if-its-not-in-the-folder/#findComment-1284801 Share on other sites More sharing options...
bruisr Posted November 4, 2011 Author Share Posted November 4, 2011 Roger. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/250385-dont-display-image-if-its-not-in-the-folder/#findComment-1284817 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.