Forskel på kode
Hej,Hvorfor virker dette:
// Update from cart
if ($_POST[task] == "updateCart"){
$i = 0;
foreach($_POST[id] as $key) {
$SESSION["cart"][$key] = $_POST[quantity][$i];
$i++;
}
}
// Recalculate cart
if ($_POST[task] == "addToCart" || $_POST[task] == "updateCart"){
$SESSION["cart"]["itemCount"] = 0;
$SESSION["cart"]["total"] = 0;
foreach($SESSION["cart"] as $key => $value) {
//echo $key." --- ".$value."<br>";
if ($key != "itemCount" && $key != "total"){
$SESSION["cart"]["itemCount"] += $SESSION["cart"][$key];
// Hent pris
$query = mysql_query("SELECT price FROM product_products WHERE productID=$key") or die(handleError($PHP_SELF, mysql_error()));
$row = mysql_fetch_array($query);
$SESSION["cart"]["total"] += $value*$row[price];
}
}
Når dette ikke virker:
function recalcCart(){
// Recalculate cart
$SESSION["cart"]["itemCount"] = 0;
$SESSION["cart"]["total"] = 0;
foreach($SESSION["cart"] as $key => $value) {
//echo $key." --- ".$value."<br>";
if ($key != "itemCount" && $key != "total"){
$SESSION["cart"]["itemCount"] += $SESSION["cart"][$key];
// Hent pris
$query = mysql_query("SELECT price FROM product_products WHERE productID=$key") or die(handleError($PHP_SELF, mysql_error()));
$row = mysql_fetch_array($query);
$SESSION["cart"]["total"] += $value*$row[price];
}
}
// Update from cart
if ($_POST[task] == "updateCart"){
$i = 0;
foreach($_POST[id] as $key) {
$SESSION["cart"][$key] = $_POST[quantity][$i];
$i++;
}
recalcCart()
}
