0

I have some function which I use in my cart application:

session_start();

if(!isset($_SESSION['products'], $_SESSION['price'], $_SESSION['product_names'],$_SESSION['product_id'])) {
     $_SESSION['products'] = 0;
     $_SESSION['price'] = 0;
     $_SESSION['product_names'] = array();
     $_SESSION['product_id'] = array();
}
if(isset($_POST['add'])) {
      $_SESSION['product_names'][] = $_POST['product_name']; 
      $_SESSION['products']++;
      $_SESSION['price'] += $_POST['price'];
}
if(isset($_POST['empty'])) {
      session_destroy();
      header('Location:index.php');
}

The question is, how can I add this into a class and call it in my view page?

I mean it will still work as <?php echo $_SESSION['price']?>. How can I make this work?

5
  • 1
    Yes, it will still work, $_SESSION is a superglobal. Commented Feb 20, 2013 at 10:17
  • ok.. well how would my class look? Commented Feb 20, 2013 at 10:18
  • Hard to tell without knowing what you want to do. Why do you need this in a class in the first place? If you want to keep on using $_POST and $_SESSION, I see nothing that can be gained by it Commented Feb 20, 2013 at 10:20
  • because i would like to keep things organized... Commented Feb 20, 2013 at 10:21
  • php.net/manual/en/language.oop5.php Commented Feb 20, 2013 at 10:21

2 Answers 2

1

Try this class you're doing in a wrong way.

session_start();    
class Shopping {


    public function __construct()
    {

    }

    //get cart products
    public function getCartProducts()
    {
        $cart = array();
        if(isset($_SESSION['cart'])) {

            $cart = unserialize($_SESSION['cart']);
        }
        return $cart;
    }

    //add product to cart
    public function addToCart($product_details)
    {
        $cart = $this->getCartProducts();
        $product_id = $product_details['product_id'];
        $is_quantity = $this->addQuantity($product_id); //check if product already exists in session if then increase the quantity
        if(!$is_quantity) {
            $product_details = array(
                                'qty' => $product_details['qty'],
                                'price' => $product_details['price'],
                                'product_name' => $product_details['name'],
                                'product_id' => $product_id
            );
            array_push($cart, $product_details);
            $_SESSION['cart'] = serialize($cart);
        }
    }

    //add quantity if product already exists
    private function addQuantity($product_id)
    {

        $cart = $this->getCartProducts();
        $i = 0;
        foreach ($cart as $product) {
            if($product['product_id'] == $product_id)
            {
                $product['qty'] += 1;
                $cart[$i] = $product;
                $_SESSION['cart'] = serialize($cart);
                return true;
            }
            $i++;
        }
        return false;
    }

    public function clearCart()
    {
        unset($_SESSION['cart']);
    }

    public function removeProduct($product_id)
    {
        $cart = $this->getCartProducts();
        $new_cart = array();
        foreach ($cart as $product) {
            if($product['product_id'] != $product_id)
                array_push($new_cart, $product);
        }
        $_SESSION['cart'] = serialize($new_cart);
    }
}

$shop = new Shopping();
$shop->addToCart(array('product_id'=>1,'name'=>'test 1','price'=>'120.00','qty'=>1));
$shop->addToCart(array('product_id'=>3,'name'=>'test 2','price'=>'120.00','qty'=>1));
Sign up to request clarification or add additional context in comments.

Comments

0

This is an example of my approach using session in class. References work pretty well.

   class Cart {
      private $container ;
      public function __construct(&$container){
        if (!isset($container) || !is_array($container))
          $session['cart'] = array() ;
        $this->container = &$container ; 
      }

      public function productExists($id){
        return (isset($this->container[$id])) ;
      }
    }

    $cart = new Cart($_SESSION['cart']) ;

At this point all changes in container can be performed in $_SESSION['cart'] . The rest of session array is not affected, that should be safe.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.