Demo of using session array to create a simple shopping cart
We will be using array commands to manage our shopping cart. We will be using three files.
cart.php : to Declare the array and adding elements
cart-display : Displaying all elements of the array
cart-remove-all: using unset command cart can be cleared.
At the starting of each page we will keep session_start() command.
Here is the code for cart.php file
<?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>
<?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>
Adding product by user to the cart
User can enter product name and add them to cart. For this a new file card-add.php is added to the script. This page shows a text box to user and on submit the data is added to shopping cart. This way users can add items to the cart without affecting the existing items in the cart.
<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']);
Session Cart with Multidimensional array
Products can have more than one attributes for the users to select. User can select quantity or colour or size while selecting a product. We will modify the above script and use multidimensional array as session variable.
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com
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
✖
We use cookies to improve your browsing experience. . Learn more