For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > May 2004 > session cart remove









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author session cart remove
paolo

2004-05-19, 8:30 am

Hi

I hope someone out there can help me.

Im developing a sessionbased shoppingcart. When adding something in
the cxart I do:
session_start();
$_SESSION["cart"][]=array("artnr" => $_GET["artnr"],
"nr" => $_GET["nr"],
"price" => $_GET["price"],
"colors" => $_GET["colors"]
);
It works fine. BUT, when I want to remove one of these element in the
cart: how should I proceed? I have read many post but dindnt find one
suitable to my case.

Thanks

Paolo
Alvaro G Vicario

2004-05-19, 8:30 am

*** paolo wrote/escribió (19 May 2004 04:28:10 -0700):
> $_SESSION["cart"][]=array(...);
> It works fine. BUT, when I want to remove one of these element in the
> cart: how should I proceed? I have read many post but dindnt find one
> suitable to my case.


Maybe you could provide an index number for the array:

$_SESSION['cart_top']++;
$_SESSION["cart"][$_SESSION['cart_top']]=array(...);



--
--
-- Álvaro G. Vicario - Burgos, Spain
--
kingofkolt

2004-05-19, 10:31 am

unset($_SESSION['cart'][$some_index]);

to be able to get the correct value for $some_index, an idea is to have each
item in your store be assigned an ID from the database that they're in
(supposing you're using one). Then have $some_index be that ID, so that you
can remove the right item from the cart when you want to.

- JP

"paolo" <paolooracle@hotmail.com> wrote in message
news:f9dc4b6.0405190328.59c847ad@posting.google.com...
> Hi
>
> I hope someone out there can help me.
>
> Im developing a sessionbased shoppingcart. When adding something in
> the cxart I do:
> session_start();
> $_SESSION["cart"][]=array("artnr" => $_GET["artnr"],
> "nr" => $_GET["nr"],
> "price" => $_GET["price"],
> "colors" => $_GET["colors"]
> );
> It works fine. BUT, when I want to remove one of these element in the
> cart: how should I proceed? I have read many post but dindnt find one
> suitable to my case.
>
> Thanks
>
> Paolo



paolooracle

2004-05-19, 12:30 pm

Yes, buit what do you do when you iterate?
Say that $_SESSION['cart_top'] has become 5.
Now you need to remove $_SESSION["cart"][3]
Then you do unset($_SESSION["cart"][3]) I guess?
What happens now? cart_top is still 5. So when you iterate
$_SESSION["cart"][3] will fail, doesnt it?

Paolo


paolooracle

2004-05-19, 12:30 pm

IS not the same problem with your solution?
I do like this now:
$_SESSION["counter"]==null ? $_SESSION["counter"]=0 :
$_SESSION["counter"]++;

$_SESSION["cart"][$_SESSION["counter"]]=array(
"artnr" => $_GET["nr"],
"price" => $_GET["price"],
"colors" => $_GET["colors"]
);


Alvaro G Vicario

2004-05-19, 12:30 pm

*** paolooracle wrote/escribió (Wed, 19 May 2004 15:04:07 GMT):
> What happens now? cart_top is still 5. So when you iterate
> $_SESSION["cart"][3] will fail, doesnt it?


foreach($_SESSION["cart"] as $index){
...
}

--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com