📘 Lesson · Lesson 43
Shopping Cart Logic
About
A shopping cart stores selected products in the session so they stay while the user browses. Here is the core logic.
Add to Cart
session_start();
$id = $_GET["id"];
if (!isset($_SESSION["cart"][$id])) {
$_SESSION["cart"][$id] = 1; // quantity
} else {
$_SESSION["cart"][$id]++;
}
echo "Added to cart";
View Cart
foreach ($_SESSION["cart"] as $id => $qty) {
echo "Product $id x $qty<br>";
}
Remove Item
unset($_SESSION["cart"][$_GET["id"]]);
echo "Removed";
Summary
- Store cart items in
$_SESSION["cart"]as id => quantity. - Add increments quantity; unset removes an item.
परिचय
Shopping cart चुने products को session में रखता है ताकि user के browse करते समय वे बने रहें। यह रहा core logic।
Cart में Add करें
session_start();
$id = $_GET["id"];
if (!isset($_SESSION["cart"][$id])) {
$_SESSION["cart"][$id] = 1; // quantity
} else {
$_SESSION["cart"][$id]++;
}
echo "Added to cart";
Cart देखें
foreach ($_SESSION["cart"] as $id => $qty) {
echo "Product $id x $qty<br>";
}
Item हटाएं
unset($_SESSION["cart"][$_GET["id"]]);
echo "Removed";
सारांश
- Cart items को
$_SESSION["cart"]में id => quantity के रूप में रखें। - Add quantity बढ़ाता है; unset item हटाता है।
💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.