Getting an image to display

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Psy
Forum Newbie
Posts: 2
Joined: Sat May 18, 2002 11:38 pm

Getting an image to display

Post by Psy »

Alright, I have an image url stored in a database for an ecommerce site. when someone goes to review an item, the image of the item is shown. but i can't get the image to show. so without further ado, here's my code:

function review_item()
{
global $review_item_id;
global $PHP_SELF;
global $browse_font_color;
$result=mysql_query("SELECT * FROM items WHERE item_id=$review_item_id");
WHILE ($row=mysql_fetch_array($result))
{
$item_name=$row["item_name"];
$item_description=$row["item_description"];
$item_price=$row["item_price"];
$image_id=$row["image_id"];
If ($image_id!='0'):
$result = mysql_query("SELECT * FROM `images` WHERE `image_id` = $image_id LIMIT 1;");
$row = mysql_fetch_row($result);
return($row[1]);
$blarg = getUrl(1);
Print "<img src=$blarg><BR>";
Endif;
Print "$item_name<BR>\n$item_description<BR>\n<BR>\n\$$item_price<BR><BR>";
Print "<a href='$PHP_SELF?buy_item_id=$review_item_id'>\n";
Print "<FONT FACE='Helvetica,Verdana,Arial' COLOR='#$browse_font_color'>";
Print "Purchase this Item";
Print "</FONT>";
Print "</a>\n";
}
}
with that code, nothing gets shown, image or the item stuff. can anyone suggest what i need to do? thanx.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

hmmm...
return($row[1]);
$blarg = getUrl(1);
Print "<img src=$blarg><BR>";
the first thing you do (before any output is done) is return.
Next (you dont do ;) ) is getting an url from the number 1 (always)
[looks like a copy&paste bug]

And then (worst of all because I like xml-style HTML ;) ) you print an picture element having a property 'src' with a NOT QUOTED value and neither closing <img> nor <br> (just kidding, but it's much easier to 'debug')
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

When using PHP to call images, you cannot select the image from the actual page and insert it there. You still need to call out to another page, such as:

Code: Select all

<img src="getImage.php?param1=1343&param2=04u5">
This would contact the getImage.php page, with parameters being sent to the page knows what image to return.

On this page, you simply do something like this:

Code: Select all

<?php
$sql = "SELECT image FROM image_tbl WHERE param1=1343 AND param2=04u5";
$result = mysql_query($sql);
list($image) = mysql_fetch_row($result);

header("Content-type: image/jpeg"); // image/gif or image/jpg
echo $image;
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

as mentioned he is only storing the url in the database.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Oh, well damn! Haha, I read "I am storing the image in the database", missing out on the 'url' part, haha.
Psy
Forum Newbie
Posts: 2
Joined: Sat May 18, 2002 11:38 pm

Post by Psy »

i used this, and it worked. thanx for the advice guys.

If ($image_id!='0'):
$query="SELECT image_url from images WHERE image_id='$review_item_id'";
$result=mysql_query($query);
$num_results=mysql_num_rows($result);
If ($num_results!=0):
$row=mysql_fetch_array($result);
$url=$row["image_url"];
echo "<img src=$url><BR>";
Else:
echo '<img src="http://www.3dmaxedout.com/na.jpg"></img>';
Endif;
//Print "<img src=$test_password><BR>";
Endif;
Post Reply