Hello Monks - I have two tables I would like to join: catalog_pet and colors. The catalog_pet tables has item_id as a unique field and the colors table has item_id and color fields. I have item_id's that have multiple colors: for example: item_id 7 has blue, green, and orange. I would like to create a popup menu that shows Item #7 with the three choice of colors. The result that I'm getting is three instances of Item 7 each with the different color. For example, Item 7 with choice of blue. Then, Item 7 with choice of green. Then, Item 7 with choice of orange. I shouldn't have Item 7 appearing three times. It should appear once with the ability to pick a color. I've pasted the necessary code used to create all this. I hope someone can give me some help..... The first section of code gathers the items within each category:
sub get_category_items { my @placeholder; my ($cat_name, $where, $limit, $value); $where = qq( WHERE catalog_pet.item_id = colors.item_id AND category = + ? ); $value = param("cat"); if ($value ne "") { push (@placeholder, $value); } shop_parameters($dbh, $cat_name, $where, @placeholder); }
Everything is passed to the next subroutine....
sub shop_parameters { my ($dbh, $cat_name, $where, @placeholder) = @_; my ($start_pos, $page_size, $max_rec); my @nav_link; # navigation link array my $limit; # LIMIT clause my @row; # summary table row array my ($stmt, $sth); my $str; my $page; # Get the page control parameters. If they're not present, this i +s # the first time we're running this search. In that case, run a q +uery # to determine the result set size and initialize the page paramet +ers. #@ INIT_PAGE_PARAMS $start_pos = param ("start_pos"); $page_size = param ("page_size"); $max_rec = param ("max_rec"); if (!defined (param ("start_pos"))) { $start_pos = 0; $page_size = 2; # change this to change #hits/page $stmt = "SELECT COUNT(*) FROM catalog_pet, colors $where"; $max_rec = $dbh->selectrow_array ($stmt, undef, @placeholder); if ($max_rec == 0) { print p ("Sorry, no qualifying listings were found."); return; } # put values into environment so nav_gen_link() can find them # (except for start_pos, which isn't constant across links) param (-name => "page_size", -value => $page_size); param (-name => "max_rec", -value => $max_rec); } #@ INIT_PAGE_PARAMS # $start_pos = number of initial records to skip # $page_size = number of records to retrieve $limit = "LIMIT $start_pos, $page_size"; print p ("$max_rec matching listings were found."); $sth = $dbh->prepare ("SELECT * FROM catalog_pet, colors $where ORDER +BY description $limit"); $sth->execute (@placeholder); $page = get_product_table ($sth); if ($page) { $page = p ({-style=>"font-family: verdana; font-size: 10pt;"}, + "Items in product category:\n" . escapeHTML (@placeholder)) . $page; } else { # if the category is empty, say so and show the category list +again $page .= p ({-style=>"font-family: verdana; font-size: 10pt;"} +, "There are no items in this product category:\n" . escapeHTML (@placeholder)); $page .= get_category_list ($dbh); } #@ BUILD_NAV_LINKS if ($max_rec > $page_size) { #@ PREV_PAGE_LINK if ($start_pos == 0) # first page: no prede +cessor { push (@nav_link, "previous"); } else { push (@nav_link, nav_gen_link ("previous", $start_pos-$pag +e_size)); } #@ PREV_PAGE_LINK for (my $i = 0; $i < $max_rec; $i += $page_size) { my $page_no = int ($i / $page_size) + 1; if ($start_pos == $i) # this is the current + page { push (@nav_link, $page_no); } else { push (@nav_link, nav_gen_link ($page_no, $i)); } } if ($start_pos+$page_size >= $max_rec) # last page: no succ +essor { push (@nav_link, "next"); } else { push (@nav_link, nav_gen_link ("next", $start_pos+$page_si +ze)); } @nav_link = map { "[$_]\n" } @nav_link; $sth->finish (); $page .= "@nav_link"; } #@ BUILD_NAV_LINKS return ($page); }
Finally, this code produces the result....
sub get_product_table { my $sth = shift; my @row; my @nav_link; while (my $ref = $sth->fetchrow_hashref ()) { my $serve_url = sprintf ("serve_image.cgi?item_id=%s;picture", es +cape ($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 => escapeHTML($ref->{item_id})), Tr ({-style=>"font-family: verdana; font-size: 10pt;"}, td (img ({-src => $serve_url, -alt => escapeHTML($ref->{item_id})}) +), td ({-width=>"80", -valign=>"top", -align => "center"}, escapeHTML( + $ref->{item_id} )), td ({-width=>"190", -valign=>"top"}, escapeHTML( $ref->{description +} ))), td (" "), Tr ({-style=>"font-family: verdana; font-size: 10pt;"}, td ({-colspan => "3", -align => "right", -style=>"font-weight: 700; + color:blue;"}, "Price: \$", escapeHTML( sprintf( "%.2f", $ref->{pri +ce}))), Tr ({-style=>"font-family: verdana; font-size: 10pt;"}, td ({-colspan => "3", -align => "right"}, "Colors:", popup_menu( -n +ame => "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/add.jpg", - +border=>"0" )))), Tr ({-valign=>"top"}, td ({-colspan=>"7", -width=>"100%"}, hr)), end_form() ); } $sth->finish (); return undef unless @row; # no items? return (table ({-border => 0, -align => "center", -style=>"font-fa +mily: verdana; font-size: 10pt;"}, @row)); }
Thanks for any help.....

update (broquaint): added <readmore> tag


In reply to 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.