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 Declar 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>

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.

cart-display.php :Displaying elements ( products )

We can display all elements or products present inside the array cart by using while loop. Here is the code.

<?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>

cart-remove-all.php : Remove all elements ( products ) from the cart

We will use unset command to remove elements from the cart. After removal we will display the total number of products available in the cart.

<?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
}

cart-remove.php Removing elements based on user selection

We can add a feature to remove elements from the cart based on user selection. For this we need to add one more page. We will say cart-remove.php . For this we need to understand handling of checkbox array and getting the checkbox array data after the form is posted.

<?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']);

Return to tutorial on Session Array

Download the above code to check the demo script at your server

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
Visitors Rating
Your Rating


Google+

hari

13-12-2014

good tutorial :) Thanks!
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 .

Post Comment This is for short comments only. Use the forum for more discussions.




Subscribe to our mailing list

* indicates required
Subscribe to plus2net
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2017 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer