Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 2.24 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. variable return null even though when return through a function it has a value
  2. public function add($id, $name, $price, $qty) {
  3.     if($this->check_basket($id)) {
  4.         $this->add_one($id);
  5.     } else {
  6.         $this->items[$id] = array(
  7.         'name' => $name,
  8.         'price' => $price,
  9.         'qauntity' => $qty
  10.         );
  11.         print_r($this->items[$id]);
  12.     }
  13. }
  14.        
  15. class Basket {
  16.  
  17.     public $items = array();
  18.  
  19.     public function __construct()
  20.     {
  21.         if($_SESSION['debug']) { echo "session construct <br/>"; }
  22.         $this->items = $_SESSION['cart'];
  23.     }
  24.  
  25.     public function check_basket($id)
  26.     {
  27.         if(array_key_exists($id, $this->items)) {
  28.             return true;
  29.  
  30.         }
  31.         return false;
  32.     }
  33.  
  34.     public function add($id, $name, $price, $qty) {
  35.         if($this->check_basket($id)) {
  36.             $this->add_one($id);
  37.         } else {
  38.             $this->items[$id] = array(
  39.                 'name' => $name,
  40.                 'price' => $price,
  41.                 'qauntity' => $qty
  42.             );
  43.             print_r($this->items[$id]);
  44.         }
  45.     }
  46.  
  47.     public function add_one($id){
  48.         $curBasket = $this->items["$id"];
  49.         $split = explode("-",$curBasket);
  50.         $split[2] = 1+$split[2];
  51.         $join = implode("-",$split);
  52.         $this->items["$id"] = $join;
  53.     }
  54.  
  55.     /*public function remove($id) {
  56.         return unset($this->items[$id]);
  57.     }*/
  58.  
  59.     public function update($id, $quantity)
  60.     {
  61.         $curBasket = $this->items["$id"];
  62.         $split = explode("-", $curBasket);
  63.         $split[2] = 1+$split[2];
  64.         $join = implode("-", $split);
  65.         $this->items["$id"] = $join;
  66.     }
  67.  
  68.     public function clear() {
  69.         unset($_SESSION['cart']);
  70.         $this->items = "";
  71.     }
  72.  
  73.     public function __destruct() {
  74.         $_SESION['cart'] = $this->items;
  75.     }
  76. }
  77.        
  78. public function __destruct() {
  79.     $_SESION['cart'] = $this->items;
  80. }
  81.        
  82. public $items;
  83.        
  84. $basket = new Basket;
  85. $basket->add('1', 'Test', '5.00', '2');
  86.  
  87. echo "<pre>n";
  88. print_r($basket);
  89. echo "</pre>n";
  90.        
  91. Basket Object
  92. (
  93.     [items] => Array
  94.         (
  95.             [1] => Array
  96.                 (
  97.                     [name] => Test
  98.                     [price] => 5.00
  99.                     [qauntity] => 2
  100.                 )
  101.  
  102.         )
  103.  
  104. )