Hi Jasonk,

I modified my add_item subroutine from hashref to arrayref and three things are occurring:
(1) when I select the first color on my list I'm receiving the following message: Can't coerce an array into a hash.
(2) when I select an item of another color, my view cart only shows item_id, quantity, and color; the description and price is not carried foward.
(3) when I try to select the same item again with a different color; it's still overwriting placing the most recent choice into the cart and removing the previous choice.

Here is how my new add_item subroutine appears when I made your modification:
sub add_item { my ($dbh, $cart_ref, $item_id, $qty, $color) = @_; # If the item isn't already in the cart, look it up from the datab +ase # and store it in the cart as a new entry with a quantity of zero. if (!exists ($cart_ref->{$item_id})) { my $sth = $dbh->prepare ("SELECT * FROM catalog_pet WHERE item_id = ?"); $sth->execute ($item_id); my $item_ref = $sth->fetchrow_arrayref (); $sth->finish (); return if !defined ($item_ref); # this shouldn't happen... $cart_ref->{$item_id} = {}; # create new entry, indexed by +item ID $cart_ref->{$item_id}->{description} = $item_ref->{description +}; $cart_ref->{$item_id}->{price} = $item_ref->{price}; $cart_ref->{$item_id}->{qty} = 1; } $cart_ref->{$item_id}->{qty} = $qty; $cart_ref->{$item_id}->{color} = $color; }
Any other thoughts or ideas would be great! Thanks

In reply to Re: Re: Need duplicate item entries in shopping cart by b310
in thread Need duplicate item entries in shopping cart 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.