can't delete all records
hi, is there anything wrong with my for loop??
<?php
for ($i=0;$i<count($cb) ;$i++)
{
array_splice($_SESSION['pid'], $i, 1);
}
?>
it works fine when i clicked on one record and delete, but when i click all to delete, it will only delete one record...
i try to echo the count($cb), when i clicked 2 checkbox, the count is 2, means there's something wrong inside the for loop izzit?? if not why it will only loop for one time only?? is there something wrong about the array_splice??
Mark Hensler posted this at 05:09 — 2nd September 2002.
He has: 4,048 posts
Joined: Aug 2000
array_splice() definition:
array array_splice ( array input, int offset [, int length [, array replacement]])
It uses an offset, not the key.
Try something like this:
<?php
foreach ($cb as $tmp) {
unset($_SESSION['pid'][$tmp]);
}
?>
Mark Hensler
If there is no answer on Google, then there is no question.
joyce posted this at 05:38 — 2nd September 2002.
They have: 164 posts
Joined: Nov 2001
i've tried unset() but it's not working. nothing is deleted. can anyone tell me wat is the appropriate function to use for this situation??
i have all my ids stored in session and when user clicked add, then it will add into the cart. i display the user cart in a table with checkboxes beside each row of record. when user clicked the checkbox and delete, it should delete the record.
i've tried using unset(), but it doesn't work for my case. none of the records are able to delete. then i switch to array_splice. by using this array, i manage to delete, but there is a problem, which is if user clicked the middle record, not the first one, but clicked the middle record, then it will delete the first record instead of the middle record.
i really donot know which function is approriate for me to use. i really need help on this. pls advice me..
zollet posted this at 17:42 — 2nd September 2002.
He has: 1,016 posts
Joined: May 2002
The reason your first code only looped once is because you need to use '<=' instead of just '<'..
<?php
for ($i=0; $i<=count($cb); $i++)
{
array_splice($_SESSION['pid'], $i, 1);
}
?>
Shouldn't you also do session_unregister('pid'); if you're cleaning the shopping cart?
Mark Hensler posted this at 03:23 — 4th September 2002.
He has: 4,048 posts
Joined: Aug 2000
The FOR loop starts at 0, so you use <. If the loop started at 1, then you use <=.
I think part of our problem is that we're only seeing half of the code each time you post. We have no idea how $_SESSION['pid'] is being populated or what is in $cb.
To debug your FOR loop, echo everything inside it. Like so:
<?php
for ($i=0; $i<count($cb); $i++) {
echo \"array_splice(\$_SESSION['pid'], $i, 1);\";
}
?>
If it does print it twice, are the values (like $i) what you expected?
I predict not...
$i is always going to be the offset of $cb, not necessarily the offset of $_SESSION['pid']. If the user selected to delete the second and fourth pid, $i will always be 1 and 2 in the FOR loop when it should be 2 and 4.
Mark Hensler
If there is no answer on Google, then there is no question.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.