Synergic Posted August 13, 2006 Share Posted August 13, 2006 I'm creating a simple, non-presistent shopping cart. Currently it allows a product ID and a qty. What i want is without re-writing the code below a simple way of adding a third variable such as a description. Any ideas? Since the array only accepts one associated field inside it i can't seem to figure how to add a third attribute.[code] <?phpclass ShoppingCart { var $items; function add_items($product_id, $qty, $desc) { $this->items[$product_id]=$qty; } function update_items($product_id, $qty) { if(array_key_exists($product_id, $this->items)) { if($this->items[$product_id]>$qty) { $this->items[$product_id]-=($this->items[$product_id]-$qty); } if($this->items[product_id]<$qty) { $this->items[$product_id]+=abs($this->items[$product_id]-$qty); } if($qty==0) { unset($this->items[product_id]); } } } function remove_item($product_id) { if(array_key_exists($product_id, $this->items)) { unset($this->items[$product_id]); } } function show_cart() { return $this->items; }}$cart = new ShoppingCart;$cart->add_items("test", "2", "3");//$cart->remove_item("Apples");$cart_items=$cart->show_cart();foreach($cart_items as $key=>$value){ echo "$key $value<br>";}?>[/code] Link to comment https://forums.phpfreaks.com/topic/17390-simple-shopping-cart-help/ Share on other sites More sharing options...
Barand Posted August 13, 2006 Share Posted August 13, 2006 Either[code]function add_items($product_id, $qty, $desc) { $this->items[$product_id] = array($qty, $desc); }[/code] in which case get description by[code]$desc = $this->items[$prod][1][/code]or[code]function add_items($product_id, $qty, $desc) { $this->items[$product_id] = array('qty' => $qty, 'desc' => $desc); }[/code] in which case get description by[code]$desc = $this->items[$prod]['desc'][/code] Link to comment https://forums.phpfreaks.com/topic/17390-simple-shopping-cart-help/#findComment-74224 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.