Jump to content


Photo

simple shopping cart help


  • Please log in to reply
1 reply to this topic

#1 Synergic

Synergic
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 13 August 2006 - 06:49 AM

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.

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






?>


#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 13 August 2006 - 09:59 PM

Either

function add_items($product_id, $qty, $desc)
    {
    
   		$this->items[$product_id] = array($qty, $desc);
    }
   
in which case get description by

$desc = $this->items[$prod][1]


or


function add_items($product_id, $qty, $desc)
    {
    
   		$this->items[$product_id] = array('qty' => $qty, 'desc' => $desc);
    }
   
in which case get description by

$desc = $this->items[$prod]['desc']

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users