PHP Shopping Cart Usage update number of cart

Source: Internet
Author: User
Tags table name

Part of the form:

The code is as follows Copy Code

<form action= "? Action=edit_num" method= "post" name= "car<?php $c _rs[' id '];? > "id=" car<?php $c _rs[' id '];? > ">

<input name= "suliang[<?php echo $c _rs[' sp_id '];? "]" type= "text" id= "suliang[<?php echo $c _rs[' sp_id '];? "" Value= "<?php echo $c _rs[' Suliang '];? > "/>

<input type= "Submit" name= "button" id= "button" value= "Update cart"/>

</form>

PHP Processing Section:

The code is as follows Copy Code

<?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 "<script>location.href= ' shopcat.php ' </script>";

}

Break

Case "NULL":

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

mysql_query ($null _sql, $conn);

echo "<script>location.href= ' shopcat.php ' </script>";

Break

Case "Del":

$id = $_get[' id '];

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

mysql_query ($del _sql, $conn);

echo "<script>location.href= ' shopcat.php ' </script>";

Break

}

?>

The above all use the database to operate, below a complete class

The code is as follows Copy Code


Class Cart {//start shopping cart class
function Check_item ($table, $session, $product) {
/*
Check items (table name, session, item)
*/
$query = SELECT * from $table WHERE session= ' $session ' and product= ' $product ';
/*
Take a look at the ' watch ' in the ' shopping cart ' for the ' product '
That is, the product has not 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, returns the number of items
There is a need to explain the Mysql_fetch_object function (which is also used below):
"Mysql_fetch_object () and mysql_fetch_array () are similar, with a little difference--returning an object instead of an array. 】
The above sentence is excerpted from the PHP manual, it should be very clear to say it ~
To put it simply, take a field from a record and use "->" instead of the subscript as an array.
*/
}
}
function Add_item ($table, $session, $product, $quantity) {
/*
Add new items (table name, session, items, quantity)
*/
$qty = $this->check_item ($table, $session, $product);
/*
Call the above function and check to see if the item has been put in the car.
*/
if ($qty = = 0) {
$query = INSERT into the $table (session, product, quantity) VALUES;
$query. = (' $session ', ' $product ', ' $quantity ');
mysql_query ($query);
/* If the car does not, then like to add the item in the car * *
} else {
$quantity + + $qty; If so, 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);
/*
Delete the item in the Cart
*/
}
function modify_quantity ($table, $session, $product, $quantity) {
/*
Modify the number of items (table name, session, items, 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) {
/*
Empty the shopping cart (nothing to say)
*/
$query = DELETE from $table WHERE session= ' $session ';
mysql_query ($query);
}
function Cart_total ($table, $session) {
/*
Total price of goods in car
*/
$query = SELECT * from $table WHERE session= ' $session ';
$result = mysql_query ($query);
/*
Get all the items out of the car first.
*/
if (mysql_num_rows ($result) > 0) {
while ($row = Mysql_fetch_object ($result)) {
/*
If the number of items is >0, determine the price and calculate
*/
$query = SELECT Price from inventory WHERE product= ' $row->product ';
$invResult = mysql_query ($query);
/*
Find the price of the item from the Inventory (inventory) table
*/
$row _price = mysql_fetch_object ($invResult);
$total + + ($row _price->price * $row->quantity);
/*
Total Price = The quantity of the item
(People should be able to see it:) )
*/
}
}
return $total; Return the total Price
}
function display_contents ($table, $session) {
/*
Get more information about all the items in the car
*/
$count = 0;
/*
Item Quantity Count
Note that this variable is not just about counting the number of items, but more importantly, it will be used as the subscript in the return value array to distinguish each item!
*/
$query = SELECT * from $table WHERE session= ' $session ' ORDER by ID;
$result = mysql_query ($query);
/*
Take all the items in the car first.
*/
while ($row = Mysql_fetch_object ($result)) {
/*
Take detailed information on each item separately
*/
$query = SELECT * from inventory WHERE product= ' $row->product ';
$result _INV = mysql_query ($query);
/*
Find information about the 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 $contents array
$contents is a two-dimensional array
The first set of subscript is to distinguish each item different information (such as item name, price, quantity, etc.)
The second set of subscripts is to distinguish between different objects (this is the role of the $count variables defined earlier)
*/
$count + +; Item quantity plus one (ie next item)
}
$total = $this->cart_total ($table, $session);
$contents [Final] = $total;
/*
At the same time, call the Cart_total function above to calculate the total price
and put it in $contents array.
*/
return $contents;
/*
Returns the array to the
*/
}
function Num_items ($table, $session) {
/*
Returns the total number of items (that is, two of the same things as a kind of nonsense--!)
*/
$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 number of database rows affected by the operation, that is, the total number of items (nothing to say)
*/
}
function Quant_items ($table, $session) {
/*
Returns the total number of items (that is, two of the same things are also two items--#)
*/
$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 quantity of the item is added to the total amount.
}
return $quant; Return Total
}
}

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.