Complete shopping code for the shopping cart instance program of PHPmysql-PHP source code

Source: Internet
Author: User
The following provides you with a complete shopping code for the PHPmysql shopping cart instance program, from database installation to warehouse receiving, and the completion of commodity transactions, okay. Let's talk a little more. Let's take a look at this code. Below we will provide you with a complete shopping code for the PHP mysql shopping cart instance program, from database installation to warehouse receiving, and the completion of commodity transactions, all of which give examples one by one, okay. Let's talk a little more. Let's take a look at this code.

Script ec (2); script

The Code is as follows:

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 tutorial ");
$ 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 */
?>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.