How to implement PHP shopping cart
This article mainly introduces the PHP shopping Cart implementation method, through 4 files to achieve the function of shopping cart, and the use of TXT file to save shopping cart content, simple and practical, the need for friends can refer to the next
This article is an example of how to implement the PHP shopping cart. Share to everyone for your reference. The specific analysis is as follows:
Here we provide you with a simple PHP shopping cart code, from the increase of shopping products and the occurrence of the purchase, in the mall development, this function is indispensable, we do not need a database, using a txt text file to operate the content of the user shopping.
Add the product to the shopping cart, the code is as follows:
The code is as follows:
//
add_item.php:
Add an item to the shopping cart.
//
Session_Start ();
if (session_is_registered (' cart ')) {
Session_register (' cart ');
}
Require ' lib.inc.php '; Loadproducts ()
Loadproducts (); Load Products in $master _products_list
Make $curr _product Global
$curr _product = Array ();
Loop through all of the products and pulling up the product
That we is interested in
foreach ($master _products_list as $prod _id = $product) {
if (Trim ($prod _id) = = Trim ($_get[id])) {
$curr _product = $product;
}
}
Register our session
Session_register (' cart ');
if (session_is_registered (' cart ')) echo "already registered";
if ($_post[ordered]) {//If they have chosen the product
Array_push ($_session[cart][products], array (Trim ($_post[id), $_post[quantity]));
$_session[cart][num_items] + = $_post[quantity];
}
?>
<title><br/><?php if ($_post[ordered]) {?> <br/> has been added <?php Echo $curr _product[name];?> to your shopping basket <b R/><?php} else {?> <br/> add <?php echo $curr _product[name];?> to your shopping basket <br/><?php} ; <br/></title>
Add to basket success
Return to the Product List page.
Add to your shopping basket
To view the shopping cart items, the code is as follows:
The code is as follows:
//
cart.php:
//
Session_Start ();
Require ' lib.inc.php ';
Determine the basket Session variable cart is registered, register the cart variable without registering
if (session_is_registered (' cart ')) {
Session_register (' cart ');
}
If the shopping basket is not initialized, initialize the shopping basket
if (!isset ($_session[cart][num_items])) {
$_session[cart] = Array ("num_items" = = 0,
"Products" = = Array ());
}
From Site_lib.inc, Loads the $master _products_list array
Loadproducts (); List of items loaded
?>
<title>The shopping basket Program for demo session tracking</title>
Welcome to the online store
if ($_session[cart][num_items]) {//If there is something to show
?>
Items currently in the shopping basket
Product Name
|
Product Description
|
Price
|
Number
|
|
Loop through the products foreach ($_session[cart][products] as $i + $product) {$product _id = $product [0]; $quanti ty = $product [1]; $total + = $quantity * (double) $master _products_list[$product _id][price];?>
|
|
|
|
}?>
Total:
|
RMB:
|
|
}
?>
Goods for sale at the store
We offer the following items for sale:
Product Name
|
Product Description
|
Price
|
|
Show all of the products foreach ($master _products_list as $product _id = $item) {?>
|
|
$
|
"> Add to Shopping basket
|
}?>
To modify the number of shopping carts, the code is as follows:
The code is as follows:
//
change_quant.php:
Change the quantity of a item in the shopping cart.
//
Session_Start ();
if (session_is_registered (' cart ')) {
Session_register (' cart ');
}
Typecast to int, making sure we access the
Right element below
$i = (int) $_post[id];
Save the old number of products for display
and arithmetic
$old _num = $_session[cart][products][$i][1];
if ($_post[quantity]) {
$_session[cart][products][$i][1] = $_post[quantity]; Change the quantity
} else {
Unset ($_session[cart][products][$i]); Send the product into oblivion
}
Update the number of items
$_session[cart][num_items] = ($old _num >$_post[quantity])?
$_session[cart][num_items]-($old _num-$_post[quantity]):
$_session[cart][num_items] + ($_post[quantity]-$old _num);
?>
<title><br/> Quantity Modified <br/></title>
Change Quantity: To
Return to the Product List page.
Function page, the user to save the contents of the shopping cart to the TXT database, the code is as follows:
The code is as follows:
Array of items
$master _products_list = Array ();
Load Item Data function
function Loadproducts () {
Global $master _products_list;
$filename = ' products.txt ';
$fp = @fopen ($filename, "R")
Or Die ("Open $filename file failed");
@flock ($FP, 1)
Or Die ("Lock $filename file failed");
Read File contents
while ($line = fgets ($fp, 1024)) {
List ($id, $name, $desc, $price) = Explode (' | ', $line); Read each row of data, data to | GE Kai
$id = Trim ($id); Remove the special symbols from the tail
$master _products_list[$id] = Array ("name" + = $name,//Name
"desc" = $desc,//description
"Price" = $price); Price
}
@fclose ($FP)//close file
Or Die ("Close $filename file failed");
}
?>
Very simple, we only use 4 files to implement the shopping cart function with PHP, OK this is just a simple PHP shopping cart code more complex need to consider more and better.
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/975888.html www.bkjia.com true http://www.bkjia.com/PHPjc/975888.html techarticle PHP Shopping Cart Implementation method This article mainly introduces the PHP shopping Cart implementation method, through 4 files to achieve the function of shopping cart, and the use of TXT file to save the contents of the shopping cart, simple and practical, the need for friends ...