TheJoey Posted August 31, 2009 Share Posted August 31, 2009 i was playing around with this simple array shopping cart, i seem to have hit a snag because i cant call up the same class . class ShoppingCart { Line 38 Fatal error: Cannot redeclare class ShoppingCart in C:\xampplite\htdocs\Trial\new2.php on line 38 <?php class ShoppingCart { var $items; function add_items($product_id, $qty) { $this->items[$product_id]=$qty; } function show_cart() { return $this->items; } } $cart = new ShoppingCart; $cart->add_items("Apples", 5); $cart->add_items("Oranges", 15); $cart->add_items("Peaches", 17); $cart_items = $cart->show_cart(); foreach($cart_items as $key => $value) { echo "Item name = $key; Item quantity: $value <br>"; } ?> After adding the items and getting an array that can be used to display them, we quickly add two more functions to remove the items from the shopping cart, and modify the quantity. <?php class ShoppingCart { var $items; function add_items($product_id, $qty) { $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("Apples", 5); $cart->add_items("Oranges", 15); $cart->add_items("Peaches", 17); $cart_items = $cart->show_cart(); foreach($cart_items as $key => $value) { echo "Item name = $key; Item quantity: $value <br>"; } $cart->update_items("Peaches", 28); $cart->update_items("Oranges", 7); $cart_items=$cart->show_cart(); echo "================<br>"; foreach($cart_items as $key=>$value) { echo "$key = $value<br>"; } $cart->remove_item("Oranges"); $cart_items=$cart->show_cart(); echo "================<br>"; foreach($cart_items as $key=>$value) { echo "$key = $value<br>"; } ?> rest of code Quote Link to comment https://forums.phpfreaks.com/topic/172528-php-arrays/ Share on other sites More sharing options...
Prismatic Posted August 31, 2009 Share Posted August 31, 2009 Remove the first instance of ShoppingCart. <?php $cart = new ShoppingCart; $cart->add_items("Apples", 5); $cart->add_items("Oranges", 15); $cart->add_items("Peaches", 17); $cart_items = $cart->show_cart(); foreach($cart_items as $key => $value) { echo "Item name = $key; Item quantity: $value <br>"; } ?> After adding the items and getting an array that can be used to display them, we quickly add two more functions to remove the items from the shopping cart, and modify the quantity. <?php class ShoppingCart { var $items; function add_items($product_id, $qty) { $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("Apples", 5); $cart->add_items("Oranges", 15); $cart->add_items("Peaches", 17); $cart_items = $cart->show_cart(); foreach($cart_items as $key => $value) { echo "Item name = $key; Item quantity: $value <br>"; } $cart->update_items("Peaches", 28); $cart->update_items("Oranges", 7); $cart_items=$cart->show_cart(); echo "================<br>"; foreach($cart_items as $key=>$value) { echo "$key = $value<br>"; } $cart->remove_item("Oranges"); $cart_items=$cart->show_cart(); echo "================<br>"; foreach($cart_items as $key=>$value) { echo "$key = $value<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/172528-php-arrays/#findComment-909494 Share on other sites More sharing options...
TheJoey Posted August 31, 2009 Author Share Posted August 31, 2009 The code isnt working at all now its just being displayed at add_items("Apples", 5); $cart->add_items("Oranges", 15); $cart->add_items("Peaches", 17); $cart_items = $cart->show_cart(); foreach($cart_items as $key => $value) { echo "Item name = $key; Item quantity: $value "; } ?> After adding the items and getting an array that can be used to display them, we quickly add two more functions to remove the items from the shopping cart, and modify the quantity. 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("Apples", 5); $cart->add_items("Oranges", 15); $cart->add_items("Peaches", 17); $cart_items = $cart->show_cart(); foreach($cart_items as $key => $value) { echo "Item name = $key; Item quantity: $value "; } $cart->update_items("Peaches", 28); $cart->update_items("Oranges", 7); $cart_items=$cart->show_cart(); echo "================ "; foreach($cart_items as $key=>$value) { echo "$key = $value "; } $cart->remove_item("Oranges"); $cart_items=$cart->show_cart(); echo "================ "; foreach($cart_items as $key=>$value) { echo "$key = $value "; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/172528-php-arrays/#findComment-909500 Share on other sites More sharing options...
Batosi Posted August 31, 2009 Share Posted August 31, 2009 You have to declare your class before you call it so place the full class above it then call it below. Just switch things around but dont try to declare it twice. Quote Link to comment https://forums.phpfreaks.com/topic/172528-php-arrays/#findComment-909506 Share on other sites More sharing options...
TheJoey Posted August 31, 2009 Author Share Posted August 31, 2009 im unsure how i would apply it. Sorry but im quiet new and ive had no major problems with php untill running into this shopping cart Quote Link to comment https://forums.phpfreaks.com/topic/172528-php-arrays/#findComment-909508 Share on other sites More sharing options...
Prismatic Posted August 31, 2009 Share Posted August 31, 2009 You have to declare your class before you call it so place the full class above it then call it below. Just switch things around but dont try to declare it twice. Not true. <?php $world = new Hello; $world->say(); class Hello { public function say() { print "Hello World!"; } } ?> Output Hello World! Quote Link to comment https://forums.phpfreaks.com/topic/172528-php-arrays/#findComment-909541 Share on other sites More sharing options...
TheJoey Posted August 31, 2009 Author Share Posted August 31, 2009 So what is it that i am doing wrong with this class. Quote Link to comment https://forums.phpfreaks.com/topic/172528-php-arrays/#findComment-909542 Share on other sites More sharing options...
trq Posted August 31, 2009 Share Posted August 31, 2009 Your defining it twice. Quote Link to comment https://forums.phpfreaks.com/topic/172528-php-arrays/#findComment-909564 Share on other sites More sharing options...
TheJoey Posted August 31, 2009 Author Share Posted August 31, 2009 So insted of using Class "classname" just use the "classname" Quote Link to comment https://forums.phpfreaks.com/topic/172528-php-arrays/#findComment-909655 Share on other sites More sharing options...
trq Posted August 31, 2009 Share Posted August 31, 2009 You cannot define the same class twice, same as you cannot define the same function twice.eg; class foo { // contents } class foo { // contents } Will cause an error. Quote Link to comment https://forums.phpfreaks.com/topic/172528-php-arrays/#findComment-909662 Share on other sites More sharing options...
emehrkay Posted August 31, 2009 Share Posted August 31, 2009 You cannot define the same class twice, same as you cannot define the same function twice.eg; Why is this thread so long? Quote Link to comment https://forums.phpfreaks.com/topic/172528-php-arrays/#findComment-909669 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.