Well, I'd say you should use this type of array:
$SESSION['cart']['qty'] = $qty;
$SESSION['cart']['prod_id'] = $prod_id;
So, your code (cleaner) can look like:
<?php
$prod_id = $_POST['prod_id'];
if (isset($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $value) {
if ($value[0] == $prod_id){
$qty = $value[1]+$_POST['qty'];
// update exixting session here
$value[1] = $qty;
} else {
$qty = $_POST['qty'];
$cart = $_SESSION['cart'];
$cart[] = array($prod_id, $qty);
$_SESSION['cart'] = $cart;
}
}
}
} else {
$qty = $_POST['qty'];
$cart = $_SESSION['cart'];
$cart[] = array($prod_id, $qty);
$_SESSION['cart'] = $cart;
}
?>
And some alternate code if that doesn't work as you expect it:
<?php
$prod_id = $_POST['prod_id'];
if (isset($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $value) {
if ($value[0] == $prod_id){
$qty = $value[1]+$_POST['qty'];
// update exixting session here
$value[1] = $qty;
} else {
$qty = $_POST['qty'];
$cart = $_SESSION['cart'];
$cart[] = array($prod_id, $qty);
$_SESSION['cart'] = array_push($_SESSION['cart'], array($prod_id, $qty));
}
}
}
} else {
$qty = $_POST['qty'];
$_SESSION['cart'] = array($prod_id, $qty);
}
?>
You'll notice I use the array_push() function to add to the current array (which is easier that doing what you were doing).
~Brett