in foreach hell

Discussion in 'PHP' started by samsung88, Sep 23, 2009.

  1. #1
    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
    
    <?php 
    $qty = -1;
    foreach($_SESSION['cart'] as $id => $value) ;
        if($id = $_GET['id']) {
            $qty = $value; 
    }
    if($qty < 0 ) {
        echo "its 0";
    } else  
    	?>
     
    PHP:
    and some output

    
    <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>
    
    PHP:
    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?
     
    samsung88, Sep 23, 2009 IP
  2. pablobablo

    pablobablo Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try
    if($id = $_GET['id'])
    Code (php):
    to replace the
    if($id == $_GET['id'])
    Code (php):
     
    Last edited: Sep 23, 2009
    pablobablo, Sep 23, 2009 IP
  3. samsung88

    samsung88 Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    not didnt work
    if($id == $_GET['id'])


    it turns quantity to a -1 for the first array

    the second (last) array works the same
     
    samsung88, Sep 23, 2009 IP
  4. superdav42

    superdav42 Active Member

    Messages:
    125
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Looks like you got a semicolon where you don´t want it to be.
    
    foreach($_SESSION['cart'] as $id => $value) ;
    
    PHP:
    The semicolon at the end of this line makes your for loop do nothing. You probably change it to :
    
    foreach($_SESSION['cart'] as $id => $value) {
    
    PHP:
    and put the closing } wherever you want your loop to end. I feel like I might be missing something because this error would imply that your cart wouldn´t work at all.
     
    superdav42, Sep 23, 2009 IP