On 10/2/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > i am using cgi session to write a cart . > here is the partial code.
If you only send part of your code, ideally you should send a part which can run on its own. That is, make a small stand-alone program which shows the problem you're having, if you can. > the problem is for loop will push mutiply times of same value if > itemID is not exit in original array. I think you mean to say that, if itemID doesn't exist in some original array, your for loop pushes many copies of the same item onto some array. > what i want is only push one times of value if itemid is a new > value Are you having trouble testing that condition, that itemID is a new value? > the cookie show below > $D = {_SESSION_ID => 'de6426230428fcf1656dd0e79bc8cf88',_SESSION_ATIME > => 1191284433,CART => [{name => '1_t-shirt',quantity => 7,price => > '14.99',itemID => '1001'}],_SESSION_EXPIRE_LIST => > {},_SESSION_REMOTE_ADDR => '192.168.1.170',_SESSION_CTIME => > 1191284098}; Cookie data like that looks as if you're storing the shopping cart data within the cookie itself. That can work, but it's open to potential abuse. (Can a potential client change the price on an item that's in the cart, simply by editing the cookie file on their system?) Also, it restricts the user to complete the transaction from that very browser, perhaps from that very session. I would generally decouple things further. I'd have a single cookie that represents the session; at my end, the session data shows which user is authenticated in that session, and what their shopping cart contains. If the user's browser quits (or crashes), they have to log in again, at which point I'd ask whether they want to continue with their saved shopping cart or to start with a new one. They may want to buy some things with a new cart, without losing their partial cart; for example, if they need to make one purchase with their work credit card and a second with another payment source. Or perhaps Dilbert fills the shopping cart with 93 items from his own computer, but it's the Pointy-Haired Boss who needs to take over the cart at his own desk to approve the order and make the payment. > use cgi::session Is that right? Is it capitalized properly? Where's the semicolon at the end of the line? > %products = ( > 1001=> [ "1_usr/bin/perl t-shirt", 14.99], > 1002 => [ "2_just perl t-shirt", 21], > 1003 => [ "3_shebang hat", 31], > 1004 => [ "4_linux mug", 41], > 1005 => [ "5_itme5", 51], > # on and on it goes.... > ); I hope you're keeping your product data and prices in a database, with this merely being example code. > sub add_item { > my ($cgi, $session, $products) = @_; > # getting the itemID to be put into the cart > my $itemID = $cgi->param("itemID") || &error ("$! ( No item > specified ) "); Is that right? Is precedence doing what you want, or do you need more parentheses? What's going to be in $! when the parameter isn't specified? Unless the param method is going to set $! (which it might, but doing so isn't standard practice) you'll find some leftover message like "bus error" that's only going to confuse whoever gets to debug your program. I suspect that you want to handle the lack of itemID in some special way. How you handle it depends upon what it means, though; what does it mean to your application that the itemID is missing? Does it mean that the user has mis-filled the form, or does it mean that your program is being invoked before the user can fill the form? > # getting the current cart's contents: > my $cart = $session->param("CART") || []; > > for my $product ( @{$cart} ) { > > if ( $product->{itemID} == $itemID) { updateitem_quantity } > else ( > push @{ $cart }, { > itemID => $itemID, > name => $products->{$itemID}->[0], > price => $products->{$itemID}->[1], > quantity => $quantity > > }; > > } That looks like problem code: you're modifying the cart (@{ $cart }) while you're in a for loop, iterating over that same array. That's a bad idea, even if it seems to work. And it doesn't seem to be working here. I think what you really intend to do there is, update the one item, but leave the others alone. In other words, the else clause code, which pushes another item onto the cart, is either useless, or it's code you intended to place elsewhere in your algorithm. Good luck with it! --Tom Phoenix Stonehenge Perl Training -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/