PHP Shopping Cart Usage update shopping cart quantity _php Tutorial

Source: Internet
Author: User
Shopping cart quantity means that when we buy things can be deleted at any time to add products, so that our shopping cart must be updated record Ah, below I will give you the PHP shopping cart update the number of shopping cart procedures and reasons, there is a need to understand the friends can refer to.

Forms section:

The code is as follows Copy Code

PHP Processing Section:

The code is as follows Copy Code
< p>!--? php

require ' config.inc.php ';

Require ' checklogin.php ';

$username = $_session[' username ');

$action = $_get[' action '];

Switch ($action) {

case ' edit_num ':

$arr = $arr = $_post[' Suliang '];

foreach ($arr as $key = + $value) {

$SQLGX = "Update ' cartemp ' Set suliang= ' $value ' where username= '". $username. " and flag=0 and Sp_id= ' ". $key." ' ";

mysql_query ($SQLGX, $conn);

Echo "";

}

Break;

Case "NULL":

$null _sql = "Delete from ' cartemp ' where username= ' $username ' and flag=0";

mysql_query ($null _sql, $conn);

Echo "";

Break;

Case "Del":

$id = $_get[' id '];

$del _sql = "Delete from ' cartemp ' where id= $id";

mysql_query ($del _sql, $conn);

Echo "";

Break;

}

?

All the above uses the database to operate, the following is a complete class

The code is as follows Copy Code


Class Cart {//start shopping cart class
function Check_item ($table, $session, $product) {
/*
Inspection items (table name, session, item)
*/
$query = SELECT * from $table WHERE session= ' $session ' and product= ' $product ';
/*
Take a look at the ' product ' in the ' shopping cart ' in ' table '
That is, 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, returns 0
*/
} else {
$row = Mysql_fetch_object ($result);
return $row->quantity;
/*
If found, returns the number of items
It is necessary to explain the Mysql_fetch_object function (which is also used below):
"Mysql_fetch_object () and mysql_fetch_array () are similar, with only a little difference--return an object instead of an array. 】
The above sentence from the PHP manual, it should be clear that the bar ~
Simply put, a field in a record should be labeled with "--" instead of as an array
*/
}
}
function Add_item ($table, $session, $product, $quantity) {
/*
Add new items (table name, session, item, quantity)
*/
$qty = $this->check_item ($table, $session, $product);
/*
Call the above function, first check if the item has been put in 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, add the item in 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 items (table name, session, item)
*/
$query = DELETE from $table WHERE session= ' $session ' and product= ' $product ';
mysql_query ($query);
/*
Remove this item from the Cart
*/
}
function modify_quantity ($table, $session, $product, $quantity) {
/*
Number of items modified (table name, session, item, quantity)
*/
$query = UPDATE $table SET quantity= ' $quantity ' WHERE session= ' $session ';
$query. = and product= ' $product ';
mysql_query ($query);
/*
Change the number of items to the value in the parameter
*/
}
function Clear_cart ($table, $session) {
/*
Empty the cart (nothing to say)
*/
$query = DELETE from $table WHERE session= ' $session ';
mysql_query ($query);
}
function Cart_total ($table, $session) {
/*
Total items in the car
*/
$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 number of items is >0, the price is judged and calculated
*/
$query = SELECT Price from inventory WHERE product= ' $row->product ';
$invResult = mysql_query ($query);
/*
Find the price of this item from the Inventory (inventory) table
*/
$row _price = mysql_fetch_object ($invResult);
$total + = ($row _price->price * $row->quantity);
/*
Total Price + = The Item value * The number of items
(You should be able to see it:) )
*/
}
}
return $total; Return Total Price
}
function display_contents ($table, $session) {
/*
Get more information about all the items in the car
*/
$count = 0;
/*
Count of items
Note that this variable is not just for counting the number of items, but more importantly, it will be used as a subscript in the array of return values to differentiate each item!
*/
$query = SELECT * from $table the WHERE session= ' $session ' ORDER by ID;
$result = mysql_query ($query);
/*
Take all the items out of the car first
*/
while ($row = Mysql_fetch_object ($result)) {
/*
Take details of each item separately
*/
$query = SELECT * from inventory WHERE product= ' $row->product ';
$result _INV = mysql_query ($query);
/*
Find information about this item from the Inventory (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 details about the item into the $contents array
$contents is a two-dimensional array
The first set of subscripts is the difference between each item's different information (such as item name, price, quantity, etc.)
The second set of subscripts is the difference between different items (this is the role of the previously defined $count variables)
*/
$count + +; 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 above cart_total function, calculate the total price
and put it in the $contents array
*/
return $contents;
/*
Returns the array to the
*/
}
function Num_items ($table, $session) {
/*
Returns the total number of item types (that is, two of the same thing counts as a nonsense--!)
*/
$query = SELECT * from $table WHERE session= ' $session ';
$result = mysql_query ($query);
$num _rows = mysql_num_rows ($result);
return $num _rows;
/*
Remove all items in the car to get the number of database rows affected by the operation, i.e. the total number of items (nothing to say)
*/
}
function Quant_items ($table, $session) {
/*
Returns the total number of items (that is, two identical items are counted as two objects--#)
*/
$quant = Total amount of 0;//items
$query = SELECT * from $table WHERE session= ' $session ';
$result = mysql_query ($query);
while ($row = Mysql_fetch_object ($result)) {
/*
Take each item out individually
*/
$quant + = $row->quantity; The amount of the item is added to the total amount.
}
return $quant; Return Total
}
}

http://www.bkjia.com/PHPjc/630715.html www.bkjia.com true http://www.bkjia.com/PHPjc/630715.html techarticle the number of shopping cart means that we can delete the work at any time when we buy items, so that our shopping cart must be updated record Ah, below I will introduce you to the PHP shopping cart update purchase ...

  • 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.