displaying pictures in perl from mysql database

O

Oli Waters

Hi everyone

I have set up a simple online shopping webpage in which a user selects
a product type and then clicks 'view products'. The data is then
retrieved from a mysql database and displayed on the page. All this
works fine apart from one part. I want to display a picture of the
product. In my database the column PRODDESC contains a link to a
picture on my hard disk. How do i code it so that it actually displays
the picture rather than displaying the link itself. Here is the
offending code....

## Execute query
my @rs = DBQuery($dbh, "SELECT PRODUCTID, PRODDESC, DATEADDED, PRICE
FROM PRODUCTS WHERE ORDERNO IS NULL AND PRODTYPE=\"$_[0]\"")
or die("DBQuery: $DBI::errstr");

## For each dataset row print in table row
foreach (@rs) {

## Set table row's background colour
$trbgcolor=($yellow)?"#F7F7DE":"white";
print "<tr height=40 style='background-color:$trbgcolor;'>";
$i=0;

## Print add to cart button and product details
print "<td align=left><input class=bluebutton
onclick='AddToCart(this);' type=button value='Add to Cart'
name='@$_[0]'></td>";
print "<td><p>@$_[1]</p></td>"; <--- IM SURE ITS THIS BIT!!
print "<td><p>@$_[2]</p></td>";

# Round price to 2 decimal places
print "<td><p>£" . sprintf("%.2f", @$_[3]) . "</p></td>";

I've labelled the thing i think is wrong...do i need an src= or
something??

Thanks in advance

Oli
 
J

James Willmore

## Execute query
my @rs = DBQuery($dbh, "SELECT PRODUCTID, PRODDESC, DATEADDED, PRICE
FROM PRODUCTS WHERE ORDERNO IS NULL AND PRODTYPE=\"$_[0]\"")
or die("DBQuery: $DBI::errstr");

## For each dataset row print in table row
foreach (@rs) {

## Set table row's background colour
$trbgcolor=($yellow)?"#F7F7DE":"white";
print "<tr height=40 style='background-color:$trbgcolor;'>";
$i=0;

## Print add to cart button and product details
print "<td align=left><input class=bluebutton
onclick='AddToCart(this);' type=button value='Add to Cart'
name='@$_[0]'></td>";
print "<td><p>@$_[1]</p></td>"; <--- IM SURE ITS THIS BIT!!
print "<td><p>@$_[2]</p></td>";

# Round price to 2 decimal places
print "<td><p>£" . sprintf("%.2f", @$_[3]) . "</p></td>";

I've labelled the thing i think is wrong...do i need an src= or
something??

To display an image on a page, you need to use the 'img' tag. Use the
location of the image as the 'src' portion of the tag. If you continue to
have problems, post to a CGI authoring group - because this is an issue
with how you're creating your HTML, not really specific to Perl :)

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
Nasrudin walked into a teahouse and declaimed, "The moon is more
useful than the sun." "Why?", he was asked. "Because at night
<we need the light more."
 
A

Andy Baxter

At earth time Sun, 11 Jan 2004 06:47:28 -0800, the following transmission
was received from the entity known as Oli Waters:
Hi everyone

I have set up a simple online shopping webpage in which a user selects
a product type and then clicks 'view products'. The data is then
retrieved from a mysql database and displayed on the page. All this
works fine apart from one part. I want to display a picture of the
product. In my database the column PRODDESC contains a link to a
picture on my hard disk. How do i code it so that it actually displays
the picture rather than displaying the link itself. Here is the
offending code....

## Execute query
my @rs = DBQuery($dbh, "SELECT PRODUCTID, PRODDESC, DATEADDED, PRICE
FROM PRODUCTS WHERE ORDERNO IS NULL AND PRODTYPE=\"$_[0]\"")
or die("DBQuery: $DBI::errstr");

## For each dataset row print in table row
foreach (@rs) {

## Set table row's background colour
$trbgcolor=($yellow)?"#F7F7DE":"white";
print "<tr height=40 style='background-color:$trbgcolor;'>";
$i=0;

## Print add to cart button and product details
print "<td align=left><input class=bluebutton
onclick='AddToCart(this);' type=button value='Add to Cart'
name='@$_[0]'></td>";
print "<td><p>@$_[1]</p></td>"; <--- IM SURE ITS THIS BIT!!
print "<td><p>@$_[2]</p></td>";

# Round price to 2 decimal places
print "<td><p>£" . sprintf("%.2f", @$_[3]) . "</p></td>";

I've labelled the thing i think is wrong...do i need an src= or
something??

Thanks in advance

Oli

Something looks odd to me in the way you've referenced the query results.
I'm guessing that DBQuery puts an array of arrayrefs into @rs, similar to
if you do $ar=$query->fetchall_arrayref; in DBI, but some things about the
code above don't sit right with that. One is that foreach without a
variable isn't defined in the perl syntax manual page, but maybe it is
part of the perl standard. Assuming that's OK, and the variable $_ gets
used instead, what you probably want is:

print "<td>${$_}[1]</td>";

by using @$_[1], I think you're referencing a whole array whose reference
is stored in the second element of @_, rather than the second element from
the array referenced by $_.

Also, the pictures need to be stored somewhere in the public directory
tree of your web or ftp server for you to be able to reference them like
this.

E.g. if you had a top-level directory called ProductImages in your web
space, then you'd want to print links like:

<img src="/ProductImages/Prod0123345.jpg">

HTH. andy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top