سلام
آیتم ها به سبد خرید اضافه میشن فقط مونده با زدن دکمه "ثبت سفارش" مقادیر (جمع کل، آیتم های سبد) در جدول ذخیره شن
در کد زیر در قسمت case insert میخوام مقادیر سشن cart_item رو insert کنم به جدول
<?phpsession_start();require_once("includes/dbcontroller.php");$db_handle = new DBController();if(!empty($_GET["action"])) {switch($_GET["action"]) { case "add": if(!empty($_POST["quantity"])) { $productByCode = $db_handle->runQuery("SELECT * FROM tbl_foods WHERE food_code='" . $_GET["code"] . "'"); $itemArray = array($productByCode[0]["food_code"]=>array('name'=>$productByCode[0]["food_name"], 'code'=>$productByCode[0]["food_code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["food_price"])); if(!empty($_SESSION["cart_item"])) { if(in_array($productByCode[0]["food_code"],array_keys($_SESSION["cart_item"]))) { foreach($_SESSION["cart_item"] as $k => $v) { if($productByCode[0]["food_code"] == $k) { if(empty($_SESSION["cart_item"][$k]["quantity"])) { $_SESSION["cart_item"][$k]["quantity"] = 0; } $_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"]; } } } else { $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray); } } else { $_SESSION["cart_item"] = $itemArray; } } break; case "insert":
break;
case "remove": if(!empty($_SESSION["cart_item"])) { foreach($_SESSION["cart_item"] as $k => $v) { if($_GET["code"] == $k) unset($_SESSION["cart_item"][$k]); if(empty($_SESSION["cart_item"])) unset($_SESSION["cart_item"]); } } break; case "empty": unset($_SESSION["cart_item"]); break; }}
?>
اینم کد های مربوط به سبد خریده
<form method="post" action="orders.php?action=success">
<?phpif(isset($_SESSION["cart_item"])){ $item_total = 0;?> <button type="submit" class="btn btn-success">ثبت سفارش</button><table class="table"><tbody>
<tr><th style="text-align:center;"><strong>نام</strong></th><th style="text-align:center;"><strong>تعداد</strong></th><th style="text-align:center;"><strong>قیمت</strong></th><th style="text-align:center;"></th></tr> <?php foreach ($_SESSION["cart_item"] as $item){ ?> <tr> <td style="text-align:center;"><strong><?php echo $item["name"]; ?></strong></td> <td style="text-align:center;"><?php echo $item["quantity"]; ?></td> <td style="text-align:center;"><?php echo $item["price"]; ?></td> <td style="text-align:center;"><a href="orders.php?action=remove&code=<?php echo $item["code"]; ?>" class="btnRemoveAction">حذف</a></td> </tr> <?php $item_total += ($item["price"]*$item["quantity"]); } ?>
<tr class="col-md-12"><td align=center style="font-weight:bold;color:green;font-size:15px;"><strong style="color:red;">مبلغ قابل پرداخت: </strong> <?php echo $item_total." تومان"; ?>
</td>
</tr><tr class="col-md-12"><td align=center><a id="btnEmpty" href="orders.php?action=empty">خالی کردن سبد</a>
</td>
</tr></tbody></table> <?php}?>