Jump to content

Archived

This topic is now archived and is closed to further replies.

Synergic

simple shopping cart help

Recommended Posts

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] <?php




class 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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

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.