We will be using array commands to manage our shopping cart. We will be using three files. <?Php
session_start();
?>
<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>Demo of Session array used for cart from plus2net.com</title>
</head>
<body>
<?Php
$_SESSION['cart']=array(); // Declaring session array
array_push($_SESSION['cart'],'apple','mango','banana'); // Items added to cart
echo "Number of Items in the cart = ".sizeof($_SESSION['cart'])." <a href=cart-remove-all.php>Remove all</a><br>";
?>
</body>
</html>
In the above code we used array_push() to add products to our shopping cart array. We have also used sizeof to count total number of elements present in our array.
<?Php
session_start();
?>
<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>Displaying Session Cart products from plus2net.com</title>
</head>
<body>
<?Php
echo "Number of Items in the cart = ".sizeof($_SESSION['cart'])." <a href=cart-remove-all.php>Remove all</a><br>";
while (list ($key, $val) = each ($_SESSION['cart'])) {
echo "$key -> $val <br>";
}
?>
</body>
</html>
<?Php
session_start();
?>
<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>Session Cart removal by plus2net.com</title>
</head>
<body>
<?Php
while (list ($key, $val) = each ($_SESSION['cart'])) {
//echo "$key -> $val <br>";
unset($_SESSION['cart'][$key]);
}
echo "Number of Items in the cart = ".sizeof($_SESSION['cart'])." <br>";
?>
</body>
</html>
In above code we are removing items from the cart and not removing the cart itself. By using the code below we can completely remove the cart.
unset($_SESSION['cart']);
After removing the cart if you try to display the items then script will give a warning message like this.
PHP Warning: Variable passed to each() is not an array or object in J:\..path....\cart-display.php on line 15
You can keep the code block inside isset() and then use.
if(isset($_SESSION[cart])){
// script to check and display elements
}else{
// message to user to add products to cart
}
<?Php
session_start();
?>
<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>Session Cart removal on selection by user at plus2net.com</title>
</head>
<body>
<?Php
$item=$_POST['item'];
while (list ($key1,$val1) = @each ($item)) {
//echo "$key1 , $val1,<br>";
unset($_SESSION['cart'][$val1]);
}
echo "Number of Items in the cart = ".sizeof($_SESSION['cart'])." <br>";
echo "<form method=post action=''>";
while (list ($key, $val) = each ($_SESSION['cart'])) {
echo " <input type=checkbox name=item[] value='$key'> $key -> $val <br>";
}
echo "<input type=submit value=Remove></form>";
?>
<a href=cart.php>Cart adding</a> . <a href=cart-display.php>Display Items</a> .<a href=cart-remove.php>Remove Item</a>
</body>
</html>
<form method=post action=''>
Enter a product name <input type=text name=product>
<input type=submit value='Add to Cart'>
</form>
<?Php
@$product=$_POST['product'];
if(strlen($product)>3){
array_push($_SESSION['cart'],$product); // Items added to cart
}
echo "<br>Number of Items in the cart = ".sizeof($_SESSION['cart']);
You can learn how to add , remove , products to a session array here.
You can download the demo script with multidimensional array here .Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| hari | 13-12-2014 |
| good tutorial :) Thanks! | |
| Artur | 16-02-2015 |
| Hallo, good tutorial to think, but I think it's kind of bad tutorial aswell, if you will use it in real shop there will be problem with array_push, for example array_push($_SESSION['cardItems'],$productID); will overwrite last added product. | |
| luigi | 23-03-2015 |
| Now that's a very useful and thank God finally a simple code, thanks! | |
| snehal | 31-10-2015 |
| how to database calling in array for cart page . | |
| Furqan | 04-09-2018 |
| how selected shopping cart item show list in cart on next page on click cart(basket) using javascript and used session without database | |
| smo1234 | 05-09-2018 |
| @Artur : array_push() will not remove any element from the array, it will only add at the end of the array. | |
19-04-2021 | |
| Amazing stuff | |