Home Backend Development PHP Tutorial PHP shopping cart example (Shenjing)_PHP tutorial

PHP shopping cart example (Shenjing)_PHP tutorial

Jul 21, 2016 pm 03:46 PM
php session the difference Example of Equivalent to shopping cart identity car

if(! $session && ! $scid) {
/*
session is used to distinguish each shopping cart, which is equivalent to the ID number of each cart;
scid is only used to identify a shopping cart id number, which can be regarded as the name of each cart;
When both the id and session value of the shopping cart do not exist, a new shopping cart is generated
*/
$session = md5( uniqid(rand()));
/*
Generate a unique shopping cart session number
rand() first generates a random number, and uniqid() then generates a unique one based on the random number string, and finally md5 the string
*/
SetCookie(scid, $session, time() + 14400);
/*
Set the shopping cart cookie
variable Name: scid (I wonder if there is a $ sign missing here? =》Correction: Add "" to scid)
Variable value: $session
Valid time: current time + 14400 seconds (within 4 hours)
For detailed usage of the setcookie function, please refer to the PHP manual~
*/
}
class Cart { //Start shopping cart class
function check_item( $table, $session, $product) {
/*
Check item (table name, session, item)
*/
$query = SELECT * FROM $table WHERE session=' $session' AND product=' $ product' ;
/*
Look at the 'table' to see if there is the 'product' in the 'shopping cart'
That is, whether the product has been put into the shopping cart
*/
$result = mysql_query( $query);
if(! $result) {
return 0;
}
/*
Query failed
*/
$ numRows = mysql_num_rows( $result);
if( $numRows == 0) {
return 0;
/*
If not found, return 0
*/
} else {
$row = mysql_fetch_object( $result);
return $row->quantity;
/*
If found, return the quantity of the item
It is necessary to explain mysql_fetch_object here Function (will be used below):
[mysql_fetch_object() is similar to mysql_fetch_array(), with one difference - it returns an object instead of an array.】
The above sentence is taken from the PHP manual, it should be very clear~
To put it simply, to get a certain field in a record, you should use "->" instead of using it like an array. Subscript
*/
}
}
function add_item( $table, $session, $product, $quantity) {
/*
Add new item (table name, session , item, quantity)
*/
$qty = $this->check_item( $table, $session, $product);
/*
Call the above function and check the class first Has the item been put into the car?
*/
if( $qty == 0) {
$query = INSERT INTO $table (session, product, quantity) VALUES ;
$query . = (' $session', ' $product', ' $quantity') ;
mysql_query( $query);
/*If it is not in the car, add the item to the car*/
} else {
$quantity += $qty; //If there is, increase the quantity on the original basis
$query = UPDATE $table SET quantity=' $quantity' WHERE session=' $session' AND ;
$query .= product=' $product' ;
mysql_query( $query);
/*
and modify the database
*/
}
}
function delete_item( $table, $session, $product) {
/*
Delete item (table name, session, item)
*/
$query = DELETE FROM $table WHERE session=' $ session' AND product=' $product' ;
mysql_query( $query);
/*
Delete items of this type in the shopping cart
*/
}
function modify_quantity( $table, $session, $product, $quantity) {
/*
Modify item quantity (table name, session, item, quantity)
*/
$query = UPDATE $table SET quantity =' $quantity' WHERE session=' $session' ;
$query .= AND product=' $product' ;
mysql_query( $query);
/*
Modify the quantity of this item For the value in the parameter
*/
}
function clear_cart( $table, $session) {
/*
Clear the shopping cart (nothing to say)
*/
$query = DELETE FROM $table WHERE session=' $session' ;
mysql_query( $query);
}
function cart_total( $table, $session) {
/*
cart Total price of items in
*/
$query = SELECT * FROM $table WHERE session=' $session' ;
$result = mysql_query( $query);
/*
First Take out all items in the car
*/
if(mysql_num_rows( $result) > 0) {
while( $row = mysql_fetch_object( $result)) {
/*
If items If the quantity is > 0 pieces, then judge the price one by one and calculate
*/
$query = SELECT price FROM inventory WHERE product=' $row->product' ;
$invResult = mysql_query( $query) ;
/*
Find the price of the item from the inventory table
*/
$row_price = mysql_fetch_object( $invResult);
$total += ( $row_price-> ;price * $row->quantity);
/*
Total price += price of the item * quantity of the item
(you should be able to understand)
*/
}
}
return $total; //Return the total price
}
function display_contents( $table, $session) {
/*
Get detailed information about all items in the car
*/
$count = 0;
/*
Item quantity count
Note that this variable is not only used to count the number of items, but more importantly, it will be used as a return value The subscript in the array is used to distinguish each item!
*/
$query = SELECT * FROM $table WHERE session=' $session' ORDER BY id ;
$result = mysql_query( $query);
/*
Take out the cart first All items in
*/
while( $row = mysql_fetch_object( $result)) {
/*
Get detailed information for each item separately
*/
$query = SELECT * FROM inventory WHERE product=' $row->product' ;
$result_inv = mysql_query( $query);
/*
Find information about this item from the inventory table
*/
$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;
/*
Put all the detailed information about the item into the $contents array
$contents is a two-dimensional array
The first set of subscripts distinguishes different information about each item (such as item name, price, quantity, etc.)
The second set of subscripts distinguishes different items (this This is the function of the $count variable defined earlier)
*/
$count++; //The number of items plus one (i.e. the next item)
}
$total = $this->cart_total( $table, $session);
$contents[final] = $total;
/*
At the same time, call the cart_total function above, calculate the total price
and put it into the $contents array
*/
return $contents;
/*
Return the array
*/
}
function num_items( $table, $session) {
/*
Returns the total number of item types (that is, it seems nonsense to count two identical items as one - -!)
*/
$query = SELECT * FROM $table WHERE session=' $session' ;
$result = mysql_query( $query);
$num_rows = mysql_num_rows( $result);
return $num_rows;
/*
Take out all the items in the car and get the effects of the operation The number of database rows, that is, the total number of items (nothing to say)
*/
}
function quant_items( $table, $session) {
/*
Returns the total number of all items (that is, , two identical things are also counted as two items - -#)
*/
$quant = 0;//Total quantity of items
$query = SELECT * FROM $table WHERE session=' $session ' ;
$result = mysql_query( $query);
while( $row = mysql_fetch_object( $result)) {
/*
Fetch each item one by one
*/
$quant += $row->quantity; //The quantity of the item is added to the total quantity
}
return $quant; //Return the total quantity
}
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320127.htmlTechArticleif(! $session ! $scid) { /* session is used to distinguish each shopping cart, which is equivalent to each The ID number of each cart; scid is only used to identify a shopping cart ID number, which can be regarded as the name of each cart;...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

See all articles