Home > Backend Development > PHP Tutorial > PHP Shopping Cart Usage: Update Shopping Cart Quantity_PHP Tutorial

PHP Shopping Cart Usage: Update Shopping Cart Quantity_PHP Tutorial

WBOY
Release: 2016-07-13 17:06:24
Original
855 people have browsed it

The shopping cart quantity means that when we buy something, we can delete and add items at any time, so our shopping cart must update the record. Now I will introduce to you the PHP shopping cart update shopping cart quantity procedure and reasons, if necessary Friends who know it can refer to it.

Form part:

The code is as follows Copy code
 代码如下 复制代码

 

 代码如下 复制代码

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;

}

?>

PHP processing part:
The code is as follows Copy code
<🎜>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; } ?>

All the above operations are done using a database. Here is 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' ;
/*
Check if the 'product' is in the 'shopping cart' in the 'table'
That is, has the product 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 item quantity
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 retrieve 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 to first check whether the item 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 there is no such item 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 this type of item 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 item quantity to the value in the parameter
*/
}
function clear_cart( $table, $session) {
/*
Clear 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 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 > 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 this 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 it:) )
*/
}
}
return $total; //Return the total price
}
function display_contents( $table, $session) {
/*
Get detailed information about everything in your 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 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 out all the items in the car first
*/
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 details about the item into the $contents array
$contents is a two-dimensional array
The first set of subscripts is to distinguish the different information of each item (such as item name, price, quantity, etc.)
The second set of subscripts is to distinguish different items (this is the role 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 to calculate the total price
and put it into the $contents array
*/
return $contents;
/*
Return this array
*/
}
function num_items( $table, $session) {
/*
Returns the total number of item types (that is, two identical items are counted as one type, which seems like 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 all items (that is, two identical items 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)) {
/*
Take out each item one by one
*/
$quant += $row->quantity; //The quantity of the item is added to the total quantity
}
return $quant; //Return the total amount
}
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630715.htmlTechArticleThe number of shopping carts means that we can delete and add items at any time when we buy things, so our shopping cart must Update record, let me introduce to you the PHP shopping cart update purchase...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template