Introduction: This is a PHP tutorial. The detailed page of the application instance. It introduces the related knowledge, skills, experience, and some PHP source code.
Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 326948 'rolling = 'no'>
PHP/MySQL shopping cartProgram
<?
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 ++;
}< br>
$ 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)/* heh, use whatever database name you put the 2 tables under in place of kmartshopper */
?>
/* Call functions like $ cart-> add_item and such, see the code .*/
More articles on "php tutorial. Application Instance"
Love J2EE follow Java Michael Jackson video station JSON online tools
Http://biancheng.dnbcw.info/php/326948.html pageno: 13.