Connecting Tech Pros Worldwide Forums | Help | Site Map

unset() is not unsetting!

Xerxes
Guest
 
Posts: n/a
#1: Jul 17 '05
Hi,
I have a cart, setup as an associative array (itemid=>qty). When the
cart is displayed, the quantity field is an input box and the value can
be changed to add/remove an item:
echo '<input type = "text" name = "'.$itemid.'" value = "'.$qty.'" size
= "3">';

when the "Save changes" button on the page that displays thecart is
pressed, a hidden field called "save" is set and the form is submitted.

session_start();
if(isset($_POST['save']))
{
foreach ($_SESSION['cart'] as $itemid => $qty)
{
$new_qty = $_POST[$optionid]; // I used (int)
$_POST[$optionid], didn't help
if($new_qty == 0) // Removing item?
{
// Search the cart for items dependent on the item being removed
if (!($result = check_required($optionid,
$_SESSION['builderid']))) { // this function returns FALSE
unset($_SESSION['cart'][$optionid]);
unset($_SESSION['optionid']);
}
.....
}
}
This things seems to go into an infinite loop. It unsets, say item 4000,
then goes back and tries to remove it again, and again. I used
error_log() function to see what is going on and it seems that the value
of the cart['itemid'] is unset but for some reason it is still there;
that particualr item keeps showing up.

Thanks for your help.




Xerxes
Guest
 
Posts: n/a
#2: Jul 17 '05

re: unset() is not unsetting!


"Xerxes" <ashkaan57@hotmail.com> wrote in message
news:21475b9193766a5eb5562f5530561c5b@news.teranew s.com...[color=blue]
> Hi,
> I have a cart, setup as an associative array (itemid=>qty). When the
> cart is displayed, the quantity field is an input box and the value[/color]
can[color=blue]
> be changed to add/remove an item:
> echo '<input type = "text" name = "'.$itemid.'" value = "'.$qty.'"[/color]
size[color=blue]
> = "3">';
>
> when the "Save changes" button on the page that displays thecart is
> pressed, a hidden field called "save" is set and the form is[/color]
submitted.[color=blue]
>
> session_start();
> if(isset($_POST['save']))
> {
> foreach ($_SESSION['cart'] as $itemid => $qty)
> {
> $new_qty = $_POST[$optionid]; // I used (int)
> $_POST[$optionid], didn't help
> if($new_qty == 0) // Removing item?
> {
> // Search the cart for items dependent on the item being[/color]
removed[color=blue]
> if (!($result = check_required($optionid,
> $_SESSION['builderid']))) { // this function returns FALSE
> unset($_SESSION['cart'][$optionid]);
> unset($_SESSION['optionid']);
> }
> ....
> }
> }
> This things seems to go into an infinite loop. It unsets, say item[/color]
4000,[color=blue]
> then goes back and tries to remove it again, and again. I used
> error_log() function to see what is going on and it seems that the[/color]
value[color=blue]
> of the cart['itemid'] is unset but for some reason it is still there;
> that particualr item keeps showing up.
>
> Thanks for your help.
>[/color]

Never mind about this issue (or non-issue), it was caused by a stupid
logic problem!


Pedro Graca
Guest
 
Posts: n/a
#3: Jul 17 '05

re: unset() is not unsetting!


Xerxes wrote:[color=blue]
> Hi,
> I have a cart, setup as an associative array (itemid=>qty). When the
> cart is displayed, the quantity field is an input box and the value can
> be changed to add/remove an item:
> echo '<input type = "text" name = "'.$itemid.'" value = "'.$qty.'" size
>= "3">';
>
> when the "Save changes" button on the page that displays thecart is
> pressed, a hidden field called "save" is set and the form is submitted.
>[/color]

turn on error reporting:

error_reporting(E_ALL);
ini_set(diplay_errors', '1');
[color=blue]
> session_start();
> if(isset($_POST['save']))
> {
> foreach ($_SESSION['cart'] as $itemid => $qty)
> {
> $new_qty = $_POST[$optionid]; // I used (int) $_POST[$optionid], didn't help[/color]

What is $optionid ?
[color=blue]
> if($new_qty == 0) // Removing item?
> {
> // Search the cart for items dependent on the item being removed
> if (!($result = check_required($optionid,
> $_SESSION['builderid']))) { // this function returns FALSE
> unset($_SESSION['cart'][$optionid]);[/color]

Changing the array that controls the loop inside the loop ?
I don't like it, but guess it's ok.
[color=blue]
> unset($_SESSION['optionid']);
> }
> ....
> }
> }
> This things seems to go into an infinite loop. It unsets, say item 4000,
> then goes back and tries to remove it again, and again. I used
> error_log() function to see what is going on and it seems that the value
> of the cart['itemid'] is unset but for some reason it is still there;
> that particualr item keeps showing up.[/color]

Perhaps you're mixing $optionid with 'optionid'
or $optionid with $itemid

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Closed Thread