


How to add Taobao to shopping cart php shopping cart example (Shen Jing)
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, uniqid() then generates a unique string based on the random number, and finally performs md5 on 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' ;
/*
Check to see if the 'product' is in the 'shopping cart' in the 'table'
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
here It is necessary to explain the mysql_fetch_object 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 fetch a certain field in a record, you should use "->" instead of using subscripts like an array
* /
}
}
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 first check whether the item of this type has been put into the car
*/
if( $qty == 0) {
$query = INSERT INTO $table (session, product, quantity) VALUES ;
$query .= (' $session', ' $product', ' $quantity') ;
mysql_query( $query);
/*If not in the car , then 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 the item to 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) {
/*
Total price of items in the cart
*/
$query = SELECT * FROM $table WHERE session =' $session' ;
$result = mysql_query( $query);
/*
Take out all the items in the car first
*/
if(mysql_num_rows( $result) > 0) {
while( $row = mysql_fetch_object ( $result)) {
/*
If the quantity of items is > 0, 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
(Everyone 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;
/*
Count the number of items
Note that this variable is not only for counting items The quantity is counted, and more importantly, it will be used as a subscript in the return value array to distinguish each item!
*/
$query = SELECT * FROM $table WHERE session=' $session' ORDER BY id ;
$result = mysql_query( $query);
/*
First take out all the items in the car
*/
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 related information about the 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 $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 is the previous The role of the defined $count variable)
*/
$count++; //Increase the number of items by one (i.e. the next item)
}
$total = $this->cart_total( $table, $session);
$contents [final] = $total;
/*
Call the cart_total function above at the same time, calculate the total price
and put it into the $contents array
*/
return $contents;
/*
Return the array
*/
}
function num_items( $table, $session) {
/*
Return 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 ones affected by 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 count as two Items - -#)
*/
$quant = 0; // Total amount of items
$query = SELECT * FROM $table WHERE session=' $session' ;
$result = mysql_query( $query);
while( $ row = mysql_fetch_object( $result)) {
/*
Take out each item one by one
*/
$quant += $row->quantity; // Add the quantity of the item to the total
}
return $ quant; //Return the total amount
}
}
The above introduces how to add Taobao to the shopping cart. PHP shopping cart example (Shen Jing), including how to add Taobao to the shopping cart. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
