dazz_club Posted February 7, 2008 Share Posted February 7, 2008 Hi guys and girls, I would like to create a function that displays an image from my database (well the image is in the folder and the name of the image is in the database). I've been reading some tutorials, Im still having trouble. If you can offer any advice that would be great. below is what i have on my index page that uses the function that i have built (or lack of) o display my image. <div class="imgHolder"> <img src="<?php echo display_home_image(); ?>" alt="hello" title="hello" > </div> Below is what is I have got the function to do, which is nothing at the moment. function display_home_image() { } Apologies for the lack of coding I have to show you guys and girls. Cheers Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 7, 2008 Share Posted February 7, 2008 Is this what you are trying to accomplish? <?php function display_home_image() { // Load Database // Run MySQL query // Parse query // Return the string of the location of the image } ?> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 7, 2008 Share Posted February 7, 2008 The src="..." parameter in the <img tag must be a URL - http://w3schools.com/html/html_images.asp For the case were php is outputting the image, you would have a separate .php file with the code to output the image and the HTML would be something like - <img src="image.php" alt=""> You cannot output image data directly in a web page using a php function or any other type of direct code, it must be referenced by a URL. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted February 7, 2008 Share Posted February 7, 2008 <?php function display_home_image() { $query = mysql_query("SELECT 'image' FROM `table`"); $row = mysql_fetch_assoc($query); return $row['image']; }?> <?php include("display_home_image.php"); $img = '<img src="'.display_home_image().'" />'; echo $img; ?> If the $row['image'] for the HOME page is something like "hello.png", and the image is in the "image/" folder then you would do <?php include("display_home_image.php"); $img = '<img src="image/'.display_home_image().'" />'; echo $img; ?> Quote Link to comment Share on other sites More sharing options...
dazz_club Posted February 7, 2008 Author Share Posted February 7, 2008 Hi there, thanks fo the replies. I'll try to reply to you all and hopefully it will make sense; @The Little Guy, your'e right thats what i want to achieve, then echo out the function in my index.php. @ PFMaBiSmAd, I think get that, I will create the code that displays the image in a different file, then put that as an include file on the main page (index.php) and then dorp in <img src="image.php" alt=""> . I will also do a but more reading. @Thanks for the code, after tyring it and making sure I made no silly errors (but theres a chance I have) when i visit my page it displays Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in E:.... I will read more upon mysql_fetch_assoc Thanks for your help guys Quote Link to comment Share on other sites More sharing options...
phpSensei Posted February 7, 2008 Share Posted February 7, 2008 Hi there, thanks fo the replies. I'll try to reply to you all and hopefully it will make sense; @The Little Guy, your'e right thats what i want to achieve, then echo out the function in my index.php. @ PFMaBiSmAd, I think get that, I will create the code that displays the image in a different file, then put that as an include file on the main page (index.php) and then dorp in <img src="image.php" alt=""> . I will also do a but more reading. @Thanks for the code, after tyring it and making sure I made no silly errors (but theres a chance I have) when i visit my page it displays Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in E:.... I will read more upon mysql_fetch_assoc Thanks for your help guys You know what Just use <?php function display_home_image() { $query = mysql_query("SELECT * FROM `table`") or die(mysql_error()); $row = mysql_fetch_array($query); return $row['image']; }?> Quote Link to comment Share on other sites More sharing options...
craygo Posted February 7, 2008 Share Posted February 7, 2008 first off if you have a lot of images you are going to have to store the names in an array. If you are looking for only one image then you are going to have to pass the id to the function. If you want all the images <?php function display_home_image() { $images = array(); $query = mysql_query("SELECT * FROM `table`") or die(mysql_error()); while($row = mysql_fetch_array($query)){ $images[] = $row['image']; } return $images; } ?> after that you would have to use a foreach statement to view them <?php $images = display_home_image(); foreach($image as $picname){ echo "<img src=\"$picname\" alt=\"\">"; } ?> If you are looking for only one pic you would use this <?php function display_home_image($picid) { $query = mysql_query("SELECT * FROM `table` WHERE `id` = '$picid'") or die(mysql_error()); $row = mysql_fetch_array($query); return $row['image']; } ?> Then call it like so <div class="imgHolder"> <img src="<?php echo display_home_image($picid); ?>" alt="hello" title="hello" > </div> Ray Quote Link to comment Share on other sites More sharing options...
dazz_club Posted February 11, 2008 Author Share Posted February 11, 2008 Hi there, Here what i have gone with so far, it works not perfect but it is a building block. function display_home_image() { $query = mysql_query("SELECT image_name FROM `images` WHERE `id` = '1'") or die(mysql_error()); $row = mysql_fetch_array($query); return $row['image_name']; } and the echo looks like this; <img src="images/<?php echo display_home_image(); ?>.png" alt="hello" title="hello" > Thanks for your help. As you had mentioned in your previous post, i have several images, so i will need to use the id i have set, along with creating the links, to select them. Thanks for your help. Quote Link to comment 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.