PHPMySQL shopping cart program instance
Last Update:2014-05-07
Source: Internet
Author: User
? If (! $ Session! $ Scid) {$ session = md5 (uniqid (rand (); SetCookie ("scid", "$ session", time () + 14400);}/* lastnumberisexpirationtimeinseconds, 14400se 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 '';