hbradshaw has asked for the wisdom of the Perl Monks concerning the following question:

Hello: I'm trying to write a subroutine to update a shopping cart. I'm running into a couple problems. One, my update cart button appears under each item. Two, When I add multiple items to the cart and want to change the quantity of one item, the update occurs to all the items. I'm not pasting any code here because there's approximately 14 pages. All I need is a second pair of eyes to see where is my error. If anyone is interested, let me know. Thanks. Helen Bradshaw

Replies are listed 'Best First'.
Re: Need some help to review code
by derby (Abbot) on Jan 09, 2003 at 20:32 UTC
    Helen,
    Looking at your update_cart sub:

    #@ UPDATE_CART sub update_cart { my ($cart_ref, $item_id, $qty) = @_; update ($cart_ref->{$item_id}->{qty} = $qty); } #@ UPDATE_CART

    That boils down to something like this if $qty is 2

    update(2);

    That may be a source of problems but I don't see the update method in your scratchpad.

    -derby

      Hi Derby, I have another bit of code that calls the update subroutine. It is in the beginning of the code. It begins like this: elsif ($choice eq "update") # update shopping cart The current subroutine that is written, using your example of 2 quantities, will change all the items in the shopping cart to 2 quantities. So, I know that the subroutine is the source of the problem. I just don't have enough Perl knowledge to fix it. I'm very new to Perl programming. I'm learning as I'm going. Any help you can give will be greatly appreciated. -Helen
        Helen,

        The part up front is this, correct?

        elsif ($choice eq "update") # update shopping cart { update_cart ($cart_ref, param("quantity")); $page .= format_cart_html ($cart_ref, 1); }

        Your upfront conditional calls the update_cart method (subroutine if you prefer), which in turn calls the method update.

        sub update_cart { my ($cart_ref, $item_id, $qty) = @_; update ($cart_ref->{$item_id}->{qty} = $qty); }

        That piece of code makes the assignment $cart_ref->{$item_id}->{qty} = $qty and then calls update with the value of $cart_ref->{$item_id}->{qty}. What I still don't see in your scratchpad is the subroutine "update." Is it part of your WebDB module or did you forget to include it. Somewhere in your code should be something like this:

        sub update { # a bunch of code }

        update: And err, you call update_cart and send it only two vars, $cart_ref and quantity; however, in the update_cart method, you say there are three parameters, $cart_ref, $item_id and $qty - thats a problem. -derby

Re: Need some help to review code
by Zaxo (Archbishop) on Jan 09, 2003 at 19:11 UTC

    Please try to narrow it down to the problem areas and post the code. Otherwise, I get paid for that sort of thing ;-)

    After Compline,
    Zaxo

      Hi Zaxo, You can view the code in my scratchpad under hbradshaw. Thanks