0

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

Edited by mangel.murti: change format

2
Contributors
4
Replies
32
Views
3 Years
Discussion Span
Last Post by mangel.murti
0

i just debug code and found solution my self. now it is working for me.hope help any one.

public function addtocart($resource,$product){

            session_start();
            $action_count=0;
            if(count($product) > 0){
                  $found=FALSE;
                    if(isset($_SESSION['cart'])){
                            foreach($_SESSION['cart'] as $key => $items){

                                       if($key == $product[0]['id']){
                                           $found=TRUE;
                                                     $action_count++;
                                                   $_SESSION['cart'][$product[0]['id']]=array('pid'=>$items['pid'],'name'=>$items['name'],'price'=>$items['price'],'qty'=>$items['qty'] +1); 
                                                   break;
                                            }

                                }


                    }else{
                           $_SESSION['cart'][$product[0]['id']]= array('pid'=>$product[0]['id'],'name'=>$product[0]['name'],'price'=>$product[0]['price'],'qty'=>1);
                             $action_count++;
                    }
                  if(!$found){
                        $_SESSION['cart'][$product[0]['id']]= array('pid'=>$product[0]['id'],'name'=>$product[0]['name'],'price'=>$product[0]['price'],'qty'=>1);
                        $action_count++;
                    }
            }
      return $action_count;
     }

Edited by mangel.murti: spell write

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.