Actually I'm learning how to use cart.php class from Ying Zang (ying@zippidesign.com):
<?
/ cart.php (c) 2000 Ying Zhang (ying@zippydesign.com)
/
class Cart {
var $items; / array of items /
var $total; / total of the cart /
var $totalidr; / total of the cart in IDR /
function Cart() {
/* object constructor */
$this->init();
}
function init() {
/* this function is called to initialize (and reset) a shopping cart */
$this->items = array();
$this->total = 0;
$this->totalidr = 0;
}
function add(&$productid, $qty) {
/* add an item to the shopping cart and update the total price */
if (isset($productid)) {
$this->items[$productid] += $qty;
}
}
function set(&$productid, $qty) {
/* set the quantity of a product in the cart to a specified value */
if (isset($productid)) {
$this->items[$productid] = (int) $qty;
}
}
function remove(&$productid) {
/* this function will remove a given product from the cart */
if (isset($productid)) {
unset($this->items[$productid]);
}
}
function cleanup() {
/* this function will clean up the cart, removing items with invalid product
* id's (non-numeric ones) and products with quantities less than 1 */
foreach ($this->items as $productid => $qty) {
if ($qty < 1) {
unset($this->items[$productid]);
}
}
}
function itemcount() {
/* returns the number of individual items in the shopping cart (note, this
* takes into account the quantities of the items as well) */
$count = 0;
foreach ($this->items as $productid => $qty) {
$count += $qty;
}
return $count;
}
function productcount() {
/* returns the number of individual products in the shopping cart */
$count = 0;
foreach ($this->items as $productid => $qty) {
$count += 1;
}
return $count;
}
function get_productid_list() {
/* return a comma delimited list of all the products in the cart, this will
* be used for queries, eg. SELECT id, price FROM products WHERE id IN ....
*/
$productid_list = "";
foreach ($this->items as $productid => $qty) {
$productid_list .= ",'" . $productid . "'";
}
/* need to strip off the leading comma */
return substr($productid_list, 1);
}
function recalc_total() {
/* recalculate the total for the shopping cart, we will also do some cleanup
* and remove invalid items from the cart. we have to query the database to
* get the prices, so instead of making one query for each product in the
* basket, we will gather up all the ID's we are interested in and run one
* query to get all the products we care about (using $in_clause) */
global $db;
$this->total = 0;
$this->totalidr = 0;
$in_clause = $this->get_productid_list();
if (empty($in_clause)) {
return;
}
$qid = mysql_query("SELECT PrID, PrRPrcUsd, PrRPrcIdr FROM Products WHERE PrID IN ($in_clause)",$db);
while ($product = mysql_fetch_object($qid)) {
$this->total += round($this->items[$product->PrID] * $product->PrRPrcUsd,2);
$this->totalidr += round($this->items[$product->PrID] * $product->PrRPrcIdr,2);
}
}
}
?>
And to add item in the shopping cart i use this:
<?
/ add_cart.php /
if(!isset($config)){include("config/config.php");} // Configuration file
if(!isset($wmlib)){include("wmlib.php");} // This is my share function
$SESSION["cart"]->add($PrID, 1);
if (! empty($HTTP_REFERER)) {
header("Location: $HTTP_REFERER");
} else {
header("Location: $base_url");
}
?>
I call add_cart.php from a link in every product detail page, sample like: cart_add.php?PrID=1
$SESSION was declared in config.php:
session_start();
session_register("SESSION");
if (!isset($SESSION)) {
$SESSION = array();
}
if (!isset($SESSION["cart"])) {
$SESSION["cart"] = new Cart;
}
That's all the code Rob, thanks for your time to help me.
Regards,
Rudy