Confuzzled Posted October 14, 2009 Share Posted October 14, 2009 Hi, I've got a site where that's got a database behind it. Currently it has loads of items in rows that all have different pictures. There is a field called "Image" that has the name of the image ie. example.jpg. Then I have a folder on my webserver called image where the pictures are stored with the same file names and extensions. Now, I want to display the correct image for the item that the page is showing. I have this code but it's just showing the usual box with a red X in the top left. I should also point out that the 'Item' line is working correctly and displaying the name of the item. <table border='1' align="left" width="100%"> <?php while($row = mysql_fetch_array( $result )) { $dir = '/image'; $image = $row['Image']; echo('<b><h1>'.$row['Item'].'</h1></b><br />'); echo "<tr>"; echo "<td width=15%>"."<img width=100% src='$dir/$image /'></td>"; echo "<td width=85% align=center>Description</td>"; echo "</tr>"; } ?> </table> Any ideas why this isn't working? Many thanks for your time! Quote Link to comment https://forums.phpfreaks.com/topic/177687-displaying-image-from-database/ Share on other sites More sharing options...
taquitosensei Posted October 14, 2009 Share Posted October 14, 2009 try viewing the source and see if it shows the correct value for the image, then try to browse to the location the source shows to see if it comes up. Quote Link to comment https://forums.phpfreaks.com/topic/177687-displaying-image-from-database/#findComment-936887 Share on other sites More sharing options...
Calver Posted October 14, 2009 Share Posted October 14, 2009 I can get this to work if I change this line... echo "<td width=15%>"."<img width=100% src='$dir/$image /'></td>"; ... to this... echo "<td width=15%>"."<img width=100% src='$dir/$image' /></td>"; Quote Link to comment https://forums.phpfreaks.com/topic/177687-displaying-image-from-database/#findComment-936890 Share on other sites More sharing options...
mrMarcus Posted October 14, 2009 Share Posted October 14, 2009 echo '<td width="15%"><img width="100%" src="'.$dir/$image.'" /></td>'; that'll work .. you have the closing / in with the image name .. make sure you use quotations in your tags or it will never validate. Quote Link to comment https://forums.phpfreaks.com/topic/177687-displaying-image-from-database/#findComment-936891 Share on other sites More sharing options...
Confuzzled Posted October 14, 2009 Author Share Posted October 14, 2009 Ok so, the example given by mrMarcus pulls me an error saying "Division by 0 in (filepath)" The example given by Calver doesn't appear to affect the page as it still remains the same without the image showing. If you both say they work, is it something to do with my browser or something? Many thanks for the oh so quick replies :D Quick EDIT: Both examples show the path as correct in the source as suggested by the first reply so i'm even more confused Quote Link to comment https://forums.phpfreaks.com/topic/177687-displaying-image-from-database/#findComment-936897 Share on other sites More sharing options...
mrMarcus Posted October 14, 2009 Share Posted October 14, 2009 my bad, this should do it: echo '<td width="15%"><img width="100%" src="'.$dir.'/'.$image.'" /></td>'; my previous code was dividing $dir by $image, which is your error. Quote Link to comment https://forums.phpfreaks.com/topic/177687-displaying-image-from-database/#findComment-936900 Share on other sites More sharing options...
Confuzzled Posted October 14, 2009 Author Share Posted October 14, 2009 my bad, this should do it: echo '<td width="15%"><img width="100%" src="'.$dir.'/'.$image.'" /></td>'; my previous code was dividing $dir by $image, which is your error. I'm sure it was, however, placing that code you just supplied in leaves the page with no image shown again Quote Link to comment https://forums.phpfreaks.com/topic/177687-displaying-image-from-database/#findComment-936901 Share on other sites More sharing options...
Calver Posted October 14, 2009 Share Posted October 14, 2009 Ok so, the example given by mrMarcus pulls me an error saying "Division by 0 in (filepath)" The example given by Calver doesn't appear to affect the page as it still remains the same without the image showing. If you both say they work, is it something to do with my browser or something? Many thanks for the oh so quick replies :D Quick EDIT: Both examples show the path as correct in the source as suggested by the first reply so i'm even more confused Sorry, I also took the '/' out of this line... $dir = 'image'; I didn't use a database for testing, just a local image. Quote Link to comment https://forums.phpfreaks.com/topic/177687-displaying-image-from-database/#findComment-936902 Share on other sites More sharing options...
mrMarcus Posted October 14, 2009 Share Posted October 14, 2009 Ok so, the example given by mrMarcus pulls me an error saying "Division by 0 in (filepath)" The example given by Calver doesn't appear to affect the page as it still remains the same without the image showing. If you both say they work, is it something to do with my browser or something? Many thanks for the oh so quick replies :D Quick EDIT: Both examples show the path as correct in the source as suggested by the first reply so i'm even more confused Sorry, I also took the '/' out of this line... $dir = 'image'; I didn't use a database for testing, just a local image. that code will show an image if there is an image to show .. check your source code and see what's being displayed in the <img> tag, and match it to what it should be. also, turn error_reporting on: error_reporting(1); at the top of your script. Quote Link to comment https://forums.phpfreaks.com/topic/177687-displaying-image-from-database/#findComment-936904 Share on other sites More sharing options...
Confuzzled Posted October 14, 2009 Author Share Posted October 14, 2009 Ok so, the example given by mrMarcus pulls me an error saying "Division by 0 in (filepath)" The example given by Calver doesn't appear to affect the page as it still remains the same without the image showing. If you both say they work, is it something to do with my browser or something? Many thanks for the oh so quick replies :D Quick EDIT: Both examples show the path as correct in the source as suggested by the first reply so i'm even more confused Sorry, I also took the '/' out of this line... $dir = 'image'; I didn't use a database for testing, just a local image. that code will show an image if there is an image to show .. check your source code and see what's being displayed in the <img> tag, and match it to what it should be. also, turn error_reporting on: error_reporting(1); at the top of your script. This worked! Thankyou very much! I will also turn on error reporting as you suggest. Cheers guys. What a great forum! I love it when people reply so quickly! Quote Link to comment https://forums.phpfreaks.com/topic/177687-displaying-image-from-database/#findComment-936905 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.