PDA

View Full Version : unregister one (not all) session..


joyce
Wed 28th Aug '02, 9:21pm
hi, i'm working on a shopping cart. i store the necesary field in session and pass it to user cart when user add the item to cart.

i do not know how to delete a session, for eg, if user adds 3 items in the cart and he wants to delete one of the item, how can i delete just one of the id instead of all the ids...

this is how i store my session:

if (isset($HTTP_POST_VARS['pid']['pid']))
{
if (is_array($_SESSION['pid']))
{
if (!in_array($HTTP_POST_VARS['pid']['pid'], $_SESSION['pid']))
{
$_SESSION['pid'][] = $HTTP_POST_VARS['pid']['pid'];
}
}else
{
$_SESSION['pid'][] = $HTTP_POST_VARS['pid']['pid'];
}
}


this is the checkbox:


print "<td width=5%><input type=checkbox name=cb[] value=\"$i\"></td>\n";


and this, when user check the checkbox and click delete:


for ($i=0; $i<count($cb); $i++)
{
unset( $_SESSION['pid'][$cb[$i]] );
}


if i have 2 or more records, and i click the first record to be deleted, it will delete all the records i have it there.

pls advice!!

Dan615
Thu 29th Aug '02, 6:13pm
Originally posted by joyce

for ($i=0; $i<count($cb); $i++)
{
unset( $_SESSION['pid'][$cb[$i]] );
}



should be


for ($i = 0; $i < count($cb); $i++) {
if (isset($cb[$i])) {
unset($_SESSION['pid'][$cb[$i]]);
}
}


That checks to make sure the box is checked for each one, and if it is then it deletes it.

joyce
Thu 29th Aug '02, 11:12pm
hi, tried the code u posted, but it gave me the same result. when i click one record, it will delete all the reocrds i have instead of just one of the record.

this is the session file(when i add 2 records into the cart):

pid|a:2:{i:0;s:1:"4";i:1;s:1:"1";}


and this is the session file when i choose one of the record to delete:

pid|a:1:{i:1;s:1:"1";}


but on my page, when choose one record to be deleted, it deletes all the records. so it shows an empty cart at my page.