Php shopping cart code
Publisher: [longlong16] Time: [2006-11-1]
<?
If (! $ Session &&! $ Scid ){
$ Session = md5 (uniqid (rand ()));
SetCookie (scid, $ session, time () + 14400 );
}/* Last number is 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 $ 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 part 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 'ventory'
#
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' not null,
Category char (1) DEFAULT 'not null,
KEY id (id ),
Primary key (id ),
KEY price (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 );
$ Cart = new Cart;
$ Mysql_link = mysql_connect (localhost, wwwrun ,);
$ Mysql_select_db (kmartShopper, $ mysql_link) 2 tables under in place of kmartShopper
// The next you can user the function of class to do something
// This is easy
?>