You've got several separate things going on here and you need to treat them separately. Putting them all together is obviously not working for you, and I can't read your code.

I think you're asking several questions:

The answer in the first case is to find or make something that uniquely identifies a product. That may be an item id, a product id, or a combination of one id and a color.

The answer in the second case is either to keep a stateful loop or to do two separate queries. I'd probably write code such as this:

sub get_colors_for_item { my ($dbh, item_id) = @_; my $sth = $dbh->prepare( 'SELECT color FROM item WHERE item_id = ? +' ); $sth->execute( $item_id ); my @colors; while( my ($color) = $sth->fetchrow_array()) { push @colors, $color; } return @colors; }

The answer to your third question is to write much smaller subroutines. Separate the database work from the printing. What's important is to build and return a data structure that makes it easy to do the printing.

I think you're getting caught up on a combinatorial explosion of details. Stop and take a deep breath. Then write some really small bits that do one simple thing at a time. Put them together -- not in one giant subroutine, but by calling them from a parent sub.

It may be an artifact of posting here, but the indentation of this code is really hard to read. perltidy can help. By default, it does a really good job of making things much more readable. You might find that your code is more maintainable that way too.


In reply to Re: Duplicate items in cart do not appear by chromatic
in thread Duplicate items in cart do not appear 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.