Location: PHPKode > scripts > Hippo Shopping Cart > hippo-shopping-cart/hippo_shopping_cart.php
<?php
/******************************************************************************
**  File        :  hippo_shopping_cart.php                                   **
**  Class name  :  hippo_shopping_cart                                       **
**  Author      :  DavidLanz                                                 **
**                                                                           **
**  Description :  This class is for shopping cart, using session store      **
**                 products_id and quantity.                                 **
**                                                                           **
**  Date          :  25/07/2005                                              **
**  Last Modified :  25/07/2005                                              **
**                                                                           **
**  PHP Version   :  5.0.4                                                   **
*******************************************************************************/

class hippo_shopping_cart
{
  var $products_id_in_cart;
  var $products_num_in_cart;
  var $num_of_products_id;
  var $num_of_products_num;
  var $final_price;
  var $user_detail;
  var $product_detail;
  
  var $err_str;      // Error Message

  // Constructor
  function hippo_shopping_cart()
  {
    $this->products_id_in_cart = Array();
    $this->products_num_in_cart = Array();
    $this->init();
  }

  function init()
  {
    if(!session_is_registered('products_id_array'))
    {
      session_register('products_id_array');
      session_register('products_quantity_array');
    }
  }
  
  function get_shopping_cart()
  {
    $this->products_id_in_cart = $_SESSION['products_id_array'];
    $this->products_num_in_cart = $_SESSION['products_quantity_array'];
    $this->num_of_products_id = count($this->products_id_in_cart);
    $this->num_of_products_num = count($this->products_num_in_cart);
  }
  
  function add_item($products_id)
  {
    $this->get_shopping_cart();
    
    if($this->num_of_products_id==0 || $this->num_of_products_num==0)
    {
      // NO Item in cart yet
      $this->products_id_in_cart[] = $products_id;
      $_SESSION['products_id_array'] = $this->products_id_in_cart;
    
      $this->products_num_in_cart[] = 1;
      $_SESSION['products_quantity_array'] = $this->products_num_in_cart;
    }
    else // cart already had products
    {
      if(!in_array($products_id, $this->products_id_in_cart)) // NOT found in cart
      {
        $this->products_id_in_cart[] = $products_id;
        $_SESSION['products_id_array'] = $this->products_id_in_cart;
    
        $this->products_num_in_cart[] = 1;
        $_SESSION['products_quantity_array'] = $this->products_num_in_cart;
      }
      else // add 1 quantity with the same products
      {
        for($i=0;$i<$this->num_of_products_id;$i++)
        {
          if($products_id==$this->products_id_in_cart[$i])
          {
            $found_products_id_index = $i;
          }
        }
        $this->products_num_in_cart[$found_products_id_index] = $this->products_num_in_cart[$found_products_id_index] +1;
        $_SESSION['products_quantity_array'] = $this->products_num_in_cart;
      }
    } // End: if($this->num_of_products_id==0 || $this->num_of_products_num==0)
    
    $this->get_shopping_cart();
    return true;
  }
  
  function update_shopping_cart($quantity_array, $if_delete_array)
  {
    if(!session_is_registered('products_id_array'))
    {
      $this->err_str += "[System Error]products_id_array Not Fount\n";
      return false;
    }
    
    $this->get_shopping_cart();
    
    $_SESSION['products_quantity_array'] = $quantity_array;
    $this->products_num_in_cart = $_SESSION['products_quantity_array'];
    
    $if_delete = $if_delete_array;
    
    for($i=0;$i<count($this->products_num_in_cart);$i++)
    {
      $check_if_delete = false;
      for($j=0;$j<count($if_delete);$j++)
      {
        if($i==$if_delete[$j])
        {
          $check_if_delete = true;
        }
      }
      if($check_if_delete!=true)
      {
        if($this->products_num_in_cart[$i]!=0)
        {
          $new_products_id_in_cart[]=$this->products_id_in_cart[$i];
          $new_products_num_in_cart[]=$this->products_num_in_cart[$i];
        }
      }
    }
  
    $_SESSION['products_id_array'] = $new_products_id_in_cart;
    $_SESSION['products_quantity_array'] = $new_products_num_in_cart;
    
    $this->get_shopping_cart();
    return true;
  }
  
  function get_shopping_cart_detail($memid, $email)
  {
    $this->get_shopping_cart();
    require('shopping_cart_DBVar.php');
    if($connection_method == "mysql")
    {
      $my_sql_1=new hippo_sql_query($connection_method,$host,$dbName1,$dbUser,$dbPass,$dbPort);
      for($i=0;$i< $this->num_of_products_id; $i++)
      {
        $sql_stmt = "SELECT f_products_audo_id,f_products_id,f_products_name,f_products_price FROM ".$dbTable1." WHERE f_products_id='".$this->products_id_in_cart[$i]."'";
        $res_1[$i]=$my_sql_1->query_exec($sql_stmt,"4");
      }
      $this->product_detail = $res_1;
      
      $my_sql_2=new hippo_sql_query($connection_method,$host,$dbName1,$dbUser,$dbPass,$dbPort);
      $sql_stmt = "SELECT f_customer_id,f_email,f_nickname FROM ".$dbTable2." WHERE f_customer_id='".$memid."' AND f_email='".$email."' ";
      $res_2 = $my_sql_2->query_exec($sql_stmt,"4");
      $this->user_detail = $res_2;
    }
    else
    {
      $this->err_str += 'system error, no connection method found...';
      return false;
    }
    
    $final_price = 0;
    for($i=0;$i<$this->num_of_products_id;$i++){$final_price = $final_price + $res_1[$i][0]['f_products_price']*$this->products_num_in_cart[$i];}
    if(!session_is_registered('final_price')){session_register('final_price');$_SESSION['final_price']=$final_price;}else{$_SESSION['final_price']=$final_price;}
    $this->final_price = $final_price;
    return true;
  }
  
  function show_error()
  {
    return $this->err_str;
  }
}
?>
Return current item: Hippo Shopping Cart