unset() is not unsetting!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Xerxes

    unset() is not unsetting!

    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

    #2
    Re: unset() is not unsetting!

    "Xerxes" <ashkaan57@hotm ail.com> wrote in message
    news:21475b9193 766a5eb5562f553 0561c5b@news.te ranews.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!


    Comment

    • Pedro Graca

      #3
      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 :

      Comment

      Working...