session_start();
Next, any where inside the page we can declare the array (if not done before ) before using. ( Start the shopping cart )
$_SESSION[cart]=array();
Here we have declared a session array. We can add elements to it by array_push() function.
array_push($_SESSION[cart],$prod_id);
We can remove items from the array by using array_diff() function.
$_SESSION[cart]=array_diff($_SESSION[cart],$prod_id);
This way we can add or remove items from out session array. We will use this in our shopping cart script.
$_SESSION['cart']=array(array("product"=>"apple","quantity"=>2),
array("product"=>"Orange","quantity"=>4),
array("product"=>"Banana","quantity"=>5),
array("product"=>"Mango","quantity"=>7),
);
$max=sizeof($_SESSION['cart']);
for($i=0; $i<$max; $i++) {
while (list ($key, $val) = each ($_SESSION['cart'][$i])) {
echo "$key -> $val ,";
} // inner array while loop
echo "<br>";
} // outer array for loop
$b=array("product"=>"$product","quantity"=>$quantity);
array_push($_SESSION['cart'],$b); // Items added to cart
unset($_SESSION['cart'][$val1]);// $val1 is the key of the element
We used above codes in our shopping cart script, you can download and check the outputs
restroika | 29-05-2010 |
How max limit of the Array Session and Array? Where can i test if the one variable is more than the max. limir? Thank. |
Mohamed Yousufdeen | 08-08-2014 |
sir,let me know how to create associative session array and how to manipulate that |
ravi | 10-03-2015 |
$_SESSION is it self array or we have to make it array |
smo | 13-03-2015 |
Yes , you can loop through and display all elements of the session array check the last part. |
uday kiran | 11-04-2016 |
if i m writing array_push($_SESSION[cart],$prod_id); in this manner the second pushed element is overwriting the first element |
smo1234 | 11-04-2016 |
No that can never happen, array_push($_SESSION['cart'],'AAA','BBB','CCC'); array_push($_SESSION['cart'],'Orange'); Second line ads fourth element to the array of cart, it is not removing any item. |
frank | 20-04-2016 |
uday kiran is right, though it is not actually overwriting. Every time the array is declared, a new empty array is created, so array_push() is in fact pushing the first $prod_id to that empty array. This can be avoid by declaring the array only if it was not already set: if(!isset($_SESSION[cart])) $_SESSION[cart]=array(); |
smo1234 | 09-05-2016 |
No that is not correct. To make it clear one more page cart-add.php is added to the script.
This page shows one input box and user can enter product through this to the cart. This will not overwrite any previously stored elements in the cart. It is adding a new one. This explanation is added in demo script also. You can download a fresh copy of the script and check. Note that we are declaring the array only once, in cart.php page. This is not to be declared in any other page. If you want to destroy this cart then you can use unset($_SESSION['cart']); Check the demo page. |
Chan | 28-11-2018 |
Could you help to do it with the codeigniter? |