Jump to content

Foreach hell!!!


phpfreaksFTW

Recommended Posts

 

 

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  
?>

 

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>

 

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?

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.