Unexpected results from a foreach loop.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bytesFTW99
    New Member
    • Nov 2008
    • 15

    #1

    Unexpected results from a foreach loop.

    i am making a little shopping cart want to make a "add to cart page" where the details of what was just added to the cart are shown. The user will be able to proceed to checkout or continue shopping.

    Item Price Qty Subtotal
    Fight 50.00 1 $50.00

    i have this code

    Code:
    <?php 
    $qty = -1;
    foreach($_SESSION['cart'] as $id => $value) ;
        if($id = $_GET['id']) {
            $qty = $value; 
    }
    if($qty < 0 ) {
        echo "its 0";
    } else  
    	?>
    and some output

    Code:
    <tr> <?php $product = find_product($id); ?>
    <td><?php echo "Id is   -->",($id); ?></td>
     <?php echo "<br>"; ?>
    <td><?php echo "Title is   -->", $product['tite']; ?></td>
     <?php echo "<br>"; ?>
    <?php echo "Price is   -->"; ?>
    <td><?php  echo number_format($product['price'],2); ?></td>
     <?php echo "<br>"; ?>
     <?php echo "Quanity is   -->"; ?>
    <td><input type="text" size="2" maxlength="2" name="<?php echo $id ?>" value="<?php echo $qty; ?>" </td>
     <?php echo "<br>"; ?>
      <?php echo "Total price is   -->"; ?>
    <td>$<?php echo number_format($product['price']* $qty, 2); ?></td>
    </tr>
    the output displays with 3 times added to the cart of the id of 1

    Id is -->1
    Title is -->Halo 3
    Price is -->55.00
    Quanity is -->3
    Total price is -->$165.00



    the array looks like this and everything is fine with 3 items in the cart with and id of 1.

    Array ( [1] => 3 )

    i add another item to the cart (id 2) and it still works fine

    Id is -->2
    Title is -->Fight Night Rd 3
    Price is -->50.00
    Quanity is -->1
    Total price is -->$50.00

    the array looks like this

    Array ( [1] => 3 [2] => 1 )

    now i go back and add another item with an id of 1 to the cart and this is where it goes bad.

    Id is -->1
    Title is -->Halo 3
    Price is -->55.00
    Quanity is -->1
    Total price is -->$55.00

    the id is right
    the title is right
    the price is right
    the quanity is wrong
    and the total price is wrong because the the quanity is wrong however the price amount is right.

    but my arrray says

    Array ( [1] => 4 [2] => 1 )

    i can not get it to go back and read the first element in the array again. it is stuck reading the 1 of [2] =>1 and can not fix it without clearing the cookies of the browser. can anyone help?
    Last edited by Atli; Sep 24 '09, 07:13 AM. Reason: Clarified the title.
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    I think that it's your $qty variable resetting. I can't be sure as you don't have the form code, or the form handling. Check your quantity, it shouldn't always be $qty = $value; (line 5) but there should be some sort of check to make sure it's not already in the array.

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hey.

      There are two problems in that foreach loop:
      [code=php]
      foreach($_SESSI ON['cart'] as $id => $value) ; // <-- #1
      if($id = $_GET['id']) { // <-- #2
      $qty = $value;
      }[/code]

      #1: The code *inside* the loop isn't really inside the loop.
      After you declare your loop, you end it prematurely by using a semi-colon. That means that the if statement, which is meant to be inside the loop (I presume), is not really inside the loop, which means it is only executed once, after the loop has already finished... looping.

      Which also means that the loop variables, $id and $value, are set to the values of the last element in the array your loop was using.

      #2: Rather then checking that the $id matches the GET element, you SET the $id to the value of the GET element.
      A single "=" sets a value. Two of them "==" check for a match.

      Which, in your case, means that the if statement will set the $id to the id in the GET element, and then set the $qty value to the $value of the last element from the foreach loop.

      This is how a foreach loop like that should look like:
      [code=php]
      foreach($array as $_key => $_value) {
      if($_key == $_GET['key']) {
      $output = $_value;
      break; // Exits the loop
      }
      }[/code]

      Also, as an alternative to your loop, you can just fetch the value of the array using the GET element.
      [code=php]<?php
      $qty = $_SESSION['cart'][$_GET['id']];

      // Or, to avoid the index out of range warning you
      // would get if the element doesn't exist:
      ($qty = @$_SESSION['cart'][$_GET['id']]) or $qty = -1;
      ?>[/code]
      Last edited by Atli; Sep 24 '09, 07:07 AM. Reason: Clarified a bit and added that last bit.

      Comment

      • bytesFTW99
        New Member
        • Nov 2008
        • 15

        #4
        thank you very much atli
        Code:
         $qty = $_SESSION['cart'][$_GET['id']];
        is exactly what i need!

        Comment

        Working...