Hi All PHP ,
Below is funtion which store product in session. $resource is database link and product just getting product from database for a given $_GET['product_id'].when i am clicking on a single product and adding that in cart it is working. if i add another product so session array is building like below. but if i add again one of product id lets say 1 to cart previous store session data at index [0] is not remaining in $_session[cart];
Array
(
[0] => Array
(
[pid] => 4
[name] => Barbi Doll
[price] => 280
[qty] => 3
)
[1] => Array
(
[pid] => 1
[name] => Dell
[price] => 555
[qty] => 1
)
)
public function addtocart($resource,$product){
#echo "<pre>";
#print_r($product);exit;
session_start();
$action_count=0;
if(count($product) > 0){
$new_product=array(array('pid'=>$product[0]['id'],'name'=>$product[0]['name'],'price'=>$product[0]['price'],'qty'=>1));
if(isset($_SESSION['cart'])){
$flag=FALSE;
foreach($_SESSION['cart'] as $items){
if($items['pid'] == $product[0]['id']){
$flag=TRUE;
$pdt= array(array('pid'=>$items['pid'],'name'=>$items['name'],'price'=>$items['price'],'qty'=>$items['qty'] +1));
}else{
$pdt= array(array('pid'=>$items['pid'],'name'=>$items['name'],'price'=>$items['price'],'qty'=>$items['qty']));
//$flag=0;
}
}
if($flag == FALSE)
{
$_SESSION['cart'] = array_merge($pdt, $new_product);
$action_count=$action_count + 1;
}else{
$_SESSION['cart'] = $pdt;
$action_count=$action_count + 1;
}
}else{
$_SESSION['cart']=$new_product;
$action_count=$action_count + 1;
}
}
return $action_count;
}
thanks for help