Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/172528-php-arrays/
Share on other sites

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>";
}


?> 

Link to comment
https://forums.phpfreaks.com/topic/172528-php-arrays/#findComment-909494
Share on other sites

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
"; } ?> 

Link to comment
https://forums.phpfreaks.com/topic/172528-php-arrays/#findComment-909500
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/172528-php-arrays/#findComment-909541
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.