<?php
require 'header.php';
if (isset ($_POST['id'])) {
$productid = $_POST['id'];
$size = $_POST['size'];
$wasfound = false;
$i = 0;
if (!isset ($_SESSION['cart']) || count($_SESSION['cart']) < 1) {
$_SESSION['cart'] = array (0 => array ("product_id" => $productid, "size" => $size, "quantity" => 1));
}
else {
foreach ($_SESSION['cart'] as $eachitem) {
$i++;
while (list ($key, $value) = each ($eachitem)) {
if (($key == "product_id" && $value == $productid) && ($key == "size" && $value == $size)) {
array_splice ($_SESSION['cart'], $i-1, 1, array (array ("product_id" => $productid, "size" => $size, "quantity" => $eachitem['quantity'] + 1)));
$wasfound = true;
}
}
}
if ($wasfound == false) {
array_push ($_SESSION['cart'], array ("product_id" => $productid, "size" => $size, "quantity" => 1));
}
}
header ("location: cart.php");
exit ();
}
?>
Everytime I add the product_id same id or different id, same size or different size, it would be the new index on array. I cannot reach array_splice condition on the script. Anyone can help? Thanks in advance
Aucun commentaire:
Enregistrer un commentaire