This article mainly introduces the php shopping cart implementation method. it implements the shopping cart function through four files and saves the content of the shopping cart using a txt file, which is simple and practical, for more information about how to implement the php shopping cart, see the example in this article. Share it with you for your reference. The specific analysis is as follows:
Here we provide you with a simple php shopping cart code, from adding shopping products and purchasing, in the mall development, this function is indispensable, we do not need a database, a txt file is used to operate the user's shopping content.
Add the product to the shopping cart. the code is as follows:
The code is as follows:
<? Php
//
// 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 the products and pull up the product
// That we are 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 "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];
}
?>
<Br/> <? Php if ($ _ POST [ordered]) {?> <Br/> added <? Php echo $ curr_product [name];?> To your shopping basket <br/> <? Php} else {?> <Br/> add <? Php echo $ curr_product [name];?> To your shopping basket <br/> <? Php }?> <Br/>
<? Php if ($ _ POST [ordered]) {?>
<? Php echo $ curr_product [name];?>
Added to shopping basket
Return to the product list page.
<? Php} else {?>
Add <? Php echo $ curr_product [name];?> To your shopping basket
<? Php }?>
View the items in the shopping cart. the code is as follows:
The code is as follows:
<? Php
//
// Cart. php:
//
Session_start ();
Require 'Lib. inc. php ';
// Determine whether cart is registered as the shopping basket session variable. if cart is not registered, the cart variable is registered.
If (session_is_registered ('cart ')){
Session_register ('cart ');
}
// If the shopping basket is not initialized, the shopping basket is initialized.
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 (); // load the item list
?>
Demonstrate the basket program for session tracking
Welcome to the online store
<? Php
If ($ _ SESSION [cart] [num_items]) {// If there is something to show
?>
Items in the current shopping basket
Product Name |
Product Description |
Unit Price |
Quantity |
|
<? Php // Loop through the productsForeach ($ _ SESSION [cart] [products] as $ I =>$ product ){$ Product_id = $ product [0];$ Quantity = $ product [1]; $ Total + = $ quantity *(Double) $ master_products_list [$ product_id] [price];?>
<? Php echo $ master_products_list [$ product_id] [name];?> |
<? Php echo $ master_products_list [$ product_id] [desc];?> |
<? Php echo $ master_products_list [$ product_id] [price];?> |
|
<? Php}?>
Total: |
RMB: <? Php echo $ total;?> |
|
<? Php
}
?>
Items to be sold in the store
We provide the following products for sale:
Product Name |
Product Description |
Unit Price |
|
<? Php// Show all of the productsForeach ($ master_products_list as $ product_id => $ item ){?>
<? Php echo $ item [name];?> |
<? Php echo $ item [desc];?> |
$ <? Php echo $ item [price];?> |
"> Add to shopping basket |
<? Php} ?>
Modify the number of cart in the following code:
The code is as follows:
<? Php
//
// Change_quant.php:
// Change the quantity of an item in the shopping cart.
//
Session_start ();
If (session_is_registered ('cart ')){
Session_register ('cart ');
}
// Typecast to int, making sure we access
// 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 );
?>
<Br/> quantity modification <br/>
Count: <? Php echo $ old_num;?> Change
<? Php echo $ _ POST [quantity];?>
Return to the product list page.
Function page, the user saves the content in the shopping cart to the txt database, the code is as follows:
The code is as follows:
<? Php
// Item array
$ Master_products_list = array ();
// Function for loading item data
Function LoadProducts (){
Global $ master_products_list;
$ Filename = 'products.txt ';
$ Fp = @ fopen ($ filename, "r ")
Or die ("opening the $ filename File failed ");
@ Flock ($ fp, 1)
Or die ("failed to lock the $ filename File ");
// Read the file content
While ($ line = fgets ($ fp, 1024 )){
List ($ id, $ name, $ desc, $ price) = explode ('|', $ line); // read data in each row, and use |
$ Id = trim ($ id); // remove the first and last special characters
$ Master_products_list [$ id] = array ("name" => $ name, // name
"Desc" => $ desc, // description
"Price" => $ price); // unit price
}
@ Fclose ($ fp) // close the file
Or die ("failed to close the $ filename File ");
}
?>
It is very simple. we only use four files to implement the shopping cart function with php. well, this is just a simple php shopping cart code that needs to be considered more complicated and better.
I hope this article will help you with php programming.