Page 1 of 1

Getting an image to display

Posted: Sat May 18, 2002 11:38 pm
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.

Posted: Sun May 19, 2002 7:19 am
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')

Posted: Sun May 19, 2002 9:07 am
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;
?>

Posted: Sun May 19, 2002 9:23 am
by volka
as mentioned he is only storing the url in the database.

Posted: Sun May 19, 2002 9:39 am
by jason
Oh, well damn! Haha, I read "I am storing the image in the database", missing out on the 'url' part, haha.

Posted: Sun May 19, 2002 11:30 am
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;