Update specific value in an array (Shopping Cart)
I am trying to create a shopping cart. I've created the add function, where if productID already exists in the basket, it'll add and extra to the quantity.
And here's my problem:
If you try to add an extra 3 of the same products to an already existing product in the basket - it'll only add 1 more, because I used: $_SESSION['kurv'][$i]['Antal']++;
How do I create it to add the $_POST['iAntal'] to the SESSION?
$iName = $_GET['iName'];
$iId = $_GET['iId'];
$iPrice = $_GET['iPrice'];
$iAntal = $_POST['iAntal'];
$action = $_GET['action'];
case "add":
if(isset($_SESSION['kurv']))
{
$laengde=sizeof($_SESSION['kurv']);
for ($i=0; $i<$laengde; $i++)
{
if (isset($_SESSION['kurv'][$i]))
{
if ($_SESSION['kurv'][$i]['ProductID'] == $iId)
{
$fundet=1;
$_SESSION['kurv'][$i]['Antal']++;
}
}
开发者_运维技巧 }
}
if (!isset($fundet))
{
$kurv = array(
'ProductID' => $iId,
'Name' => $iName,
'Price' => $iPrice,
'Antal' => $iAntal
);
$_SESSION['kurv'][] = $kurv;
}
break;
$_SESSION['kurv'][$i]['Antal'] += (int) $_POST['iAntal'];
You can add many products with this:
$quantity = $_GET["quantity"];
$_SESSION['kurv'][$i]['Antal'] = $_SESSION['kurv'][$i]['Antal']+$quantity;
精彩评论