displaying pictures in perl from mysql database

Discussion in 'Perl Misc' started by Oli Waters, Jan 11, 2004.

  1. Oli Waters

    Oli Waters Guest

    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
     
    Oli Waters, Jan 11, 2004
    #1
    1. Advertising

  2. Oli Waters

    Matt Garrish Guest

    "Oli Waters" <> wrote in message
    news:...
    >
    > How do i code it so that it actually displays
    > the picture rather than displaying the link itself.


    Most people would use an <img> tag to display graphics. Seems like you need
    a good html book, or you could always try the w3 site www.w3.org or the
    w3schools site: http://www.w3schools.com/.

    Matt
     
    Matt Garrish, Jan 11, 2004
    #2
    1. Advertising

  3. On Sun, 11 Jan 2004 06:47:28 -0800, Oli Waters wrote:

    > ## 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."
     
    James Willmore, Jan 11, 2004
    #3
  4. Oli Waters

    Andy Baxter Guest

    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.

    --
    http://www.niftybits.ukfsn.org/

    remove 'n-u-l-l' to email me. html mail or attachments will go in the spam
    bin unless notified with
    HTML:
     or [attachment] in the subject line.
     
    Andy Baxter, Jan 12, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.onet.pl
    Replies:
    1
    Views:
    313
  2. Martin Raychev
    Replies:
    1
    Views:
    303
    Alvin Bruney [MVP]
    Mar 2, 2004
  3. qqqq

    Pictures not displaying

    qqqq, Jan 19, 2005, in forum: ASP General
    Replies:
    4
    Views:
    134
  4. Oli Waters
    Replies:
    2
    Views:
    272
    Andy Baxter
    Jan 12, 2004
  5. David. E. Goble

    displaying a list of pictures?

    David. E. Goble, May 21, 2005, in forum: Javascript
    Replies:
    3
    Views:
    102
    Random
    May 23, 2005
Loading...

Share This Page