if (! $session &&! $scid) { $session = MD5 (Uniqid (rand ()); Setcookie ("SCID", "$session", Time () + 14400); }/* Last number are expiration time in seconds, 14400 sec = 4 hrs * * Class Cart { function Check_item ($table, $session, $product) { $query = "SELECT * from $table WHERE session= ' $session ' and product= ' $product '"; $result = mysql_query ($query); if (! $result) { return 0; } $numRows = mysql_num_rows ($result); if ($numRows = = 0) { return 0; } else { $row = Mysql_fetch_object ($result); return $row->quantity; } } function Add_item ($table, $session, $product, $quantity) { $qty = $this->check_item ($table, $session, $product); if ($qty = = 0) { $query = "INSERT into the $table (session, product, quantity) VALUES"; $query. = "(' $session ', ' $product ', ' $quantity ')"; mysql_query ($query); } else { $quantity + + $qty; $query = "UPDATE $table SET quantity= ' $quantity ' WHERE session= ' $session ' and '; $query. = "product= ' $product '"; mysql_query ($query); } } function Delete_item ($table, $session, $product) { $query = "DELETE from $table WHERE session= ' $session ' and product= ' $product '"; mysql_query ($query); } function modify_quantity ($table, $session, $product, $quantity) { $query = "UPDATE $table SET quantity= ' $quantity ' WHERE session= ' $session '"; $query. = "and product= ' $product '"; mysql_query ($query); } function Clear_cart ($table, $session) { $query = "DELETE from $table WHERE session= ' $session '"; mysql_query ($query); } function Cart_total ($table, $session) { $query = "SELECT * from $table WHERE session= ' $session '"; $result = mysql_query ($query); if (mysql_num_rows ($result) > 0) { while ($row = Mysql_fetch_object ($result)) { $query = "Select Price from inventory WHERE product= ' $row->product '"; $invResult = mysql_query ($query); $row _price = mysql_fetch_object ($invResult); $total + + ($row _price->price * $row->quantity); } } return $total; } Function display_contents ($table, $session) { $count = 0; $query = "SELECT * from $table WHERE session= ' $session ' ORDER by id"; $result = mysql_query ($query); while ($row = Mysql_fetch_object ($result)) { $query = "SELECT * FROM inventory WHERE product= ' $row->product '" ; $result _INV = mysql_query ($query); $row _inventory = mysql_fetch_object ($result _inv); $contents [Product] [$count] = $row _inventory->product; $contents ["Price"] [$count] = $row _inventory->price; $contents ["Quantity"] [$count] = $row->quantity; $contents ["Total"] [$count] = ($row _inventory->price * $row->quantity); $contents ["description"] [$count] = $row _inventory->description; $count + +; } $total = $this->cart_total ($table, $session); $contents ["final"] = $total; return $contents; } function Num_items ($table, $session) { $query = "SELECT * from $table WHERE session= ' $session '"; $result = mysql_query ($query); $num _rows = mysql_num_rows ($result); return $num _rows; } function Quant_items ($table, $session) { $quant = 0; $query = "SELECT * from $table WHERE session= ' $session '"; $result = mysql_query ($query); while ($row = Mysql_fetch_object ($result)) { $quant + + $row->quantity; } return $quant; } } ?> /* This section contains a description of how to create the tables on your MySQL server. # MySQL Dump 6.0 # # Host:localhost Database:kmartshopper #-------------------------------------------------------- # Server Version 3.22.25 # # Table structure for table ' inventory ' # CREATE TABLE Inventory ( Product Tinytext not NULL, Quantity Tinytext not NULL, ID int (4) DEFAULT ' 0 ' not NULL auto_increment, Description Tinytext not NULL, Price float (10,2) DEFAULT ' 0.00 ' is not NULL, Category char (1) DEFAULT ' not NULL, KEY ID (ID), PRIMARY KEY (ID), KEY Price ); # # Table structure for table ' shopping ' # CREATE TABLE Shopping ( Session Tinytext not NULL, Product Tinytext not NULL, Quantity Tinytext not NULL, Card Tinytext not NULL, ID int (4) DEFAULT ' 0 ' not NULL auto_increment, KEY ID (ID), PRIMARY KEY (ID) ); */ Example . Include ("shoppingcart.php tutorial"); $cart = new cart; $mysql _link = mysql_connect ("localhost", "Wwwrun", ""); $mysql _select_db ("Kmartshopper", $mysql _link)/* Heh, use whatever database name to put the 2 tables under of Kmartshopper */ ? |