Okay, a brief lesson on references: you can do anything to the array pointed at by a reference that you could do to the array itself. So, the following two snippets do exactly the same thing (one to an array, the other to a referenced array):

# create and fool around with an array my @rray = (); $rray[0] = "foo"; push @rray, "bar"; print $rray[1], "\n"; # prints "bar" # create and fool around with an arrayref my $arrayref = []; $arrayref->[0] = "foo"; push @$arrayref, "bar"; print $arrayref->[1], "\n"; #prints "bar"

So as you can see, using an arrayref is just an indirect way of using an array, with the added benefit that the reference can be passed around like a scalar, and put into arrays and hashes, and all sorts of fun stuff like that.

So, back to your problem. You wind up with $ref (which happens to be a reference to a hash). You're already pulling out values like $ref->{item_id} and $ref->{description}, so now you need to make $ref->{color} an arrayref and iterate over it. Here's the part of your code that takes the record set and turns it into a table. The HTML looks kinda b0rked... Please forgive me if I screw it up more.

while (my $ref = $sth->fetchrow_hashref ()) { ### NEW CODE ### # Start up a new query $clref = $dbh->prepare ("SELECT color FROM colors WHERE item_id = +".$ref->{item_id}); $clref->execute; # Push each new colour onto the array referenced by # $ref->{color} while (my $color = $clref->fetchrow_hashref()) { push @{$ref->{color}}, $color->{color}; } # Now $ref->{color} is an arrayref full of colours. ### ### ### ### my $serve_url = sprintf ("serve_image.cgi?item_id=%s;picture", esc +ape ($ref->{item_id})); # generate a form allowing a quantity of the item to be added # to the cart push (@row, start_form(-method=>'POST', -action=>url())), hidden( -name => "choice", -override => 1, -default => "add" ) +, hidden( -name => "item_id", -override => 1, -default => escape +HTML($ref->{item_id})), Tr ({-style=>"font-family: verdana; font-size: 10pt;"}, td ( img ({-src => $serve_url, -alt => escapeHTML($ref->{it +em_id})}) ), td ({-width=>"80", -valign=>"top", -align => "center"}, escapeHTML( $ref->{item_id} ) ), td ({-width=>"190", -valign=>"top"}, escapeHTML( $ref->{description} ) ) ), Tr ({-style=>"font-family: verdana; font-size: 10pt;"}, td ({-colspan => "3", -align => "right", -style=>"font-wei +ght: 700; color:blue;"}, "Price: \$", escapeHTML( sprintf( "%.2f", $ref->{price}) ) ) ), Tr ({-style=>"font-family: verdana; font-size: 10pt;"}, # Here is where you need to RTFM. At this point, # $ref->{color} is an arrayref. I suspect that # the popup_menu procedure will accept an array # reference as an argument, but I'm not sure. # Also, I'm too lazy to RTFM myself :o) In any # case, if you need to give it an array instead # you can dereference the arrayref. Check the # links below for how to do that. td ({-colspan => "3", -align => "right"}, "Colors:", popup_menu( -name => "color", -values => $ref->{color} +) ) ), Tr ({-style=>"font-family: verdana; font-size: 10pt;"}, td ({-colspan => "3", -align => "right"}, "Quantity:", popup_menu( -name => "quantity", -values => ["1", "2", + "3", "4", "5", "6", "7", "8", "9"]) ), td ( image_button( -name => "Add Item", -src=>"../images/ad +d.jpg", -border=>"0" ) ) ), Tr ({-valign=>"top"}, td ({-colspan=>"7", -width=>"100%"}, hr ) ), end_form() ); }

For more information on references and how to use them (from people who know a whole lot more than me) See:
The 'references' tutorial by busunsl
References quick reference by tye

LAI
:eof


In reply to Re^5: Joining Tables Problem by LAI
in thread Joining Tables Problem by b310

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.