in reply to Re: Need some help to review code
in thread Need some help to review code

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

Replies are listed 'Best First'.
Re: Re: Re: Need some help to review code
by derby (Abbot) on Jan 10, 2003 at 01:28 UTC
    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

      Hi Derby, Thank you for responding. I see what you mean. No, I do not have a subroutine called update. At this point, I'm not sure how I should write it. At least I know what's missing. Hopefully, if I can figure out the proper code for the update method, this will all work. I do see your point with the three parameters in the update_cart method versus the two parameters when I call update_cart. That I will need to fix. Thanks for your help. Helen
      Hi Derby, I think I found the root of my problem. It is located in the sub format_cart_html section of the code. In this subroutine I created a form to post a new quantity when entered in the quantity. Here's what's happening: I add an item with a quantity of 2 to my cart. I then add another item with a quantity of 5 to my cart. When I add the item with 5, it also changes my item of quantity 2 to a 5. It appears to me that the form I created and the parameters I am using are overriding the values of previous items that were added with the most recent value that is being added. Can you take a look at this section of code and give me some opinions? Thanks. Helen