Jump to content

Display an image


dazz_club

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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;
?>

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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'];

}?>

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.