2013-02-20 110 views
0

我有一些功能,我在我的购物车应用程序中使用:添加代码片段到一个类

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'); 
} 

问题是,我怎么能添加到这个类,并把它在我看来,页面?

我的意思是它仍然会作为<?php echo $_SESSION['price']?>。我该如何做这项工作?

+1

是的,它仍然可以工作,'$ _SESSION'是一个超全球性的。 – 2013-02-20 10:17:10

+0

好的..我的课堂怎么样? – emcee22 2013-02-20 10:18:14

+0

很难说不知道你想做什么。为什么你首先在课堂上需要这个?如果你想继续使用'$ _POST'和'$ _SESSION',我看不到任何可以获得的东西 – 2013-02-20 10:20:15

回答

1

试试这个课程你做错了。

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)); 
0

这是我在课堂上使用session的方法的一个例子。 References工作得很好。

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']) ; 

此时容器的所有更改都可以在$_SESSION['cart']中执行。会话数组的其余部分不受影响,应该是安全的。