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

Recommended Answers

All 4 Replies

To add more items you could simply do $myArray[] = array();

how to do with above function i made array there. can u see that?

I've only briefly scanned it, try this:

$pdt[] = $new_product;

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;
     }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.