Fragment 3. Create a new basket and add a project to it
Set Item count to 1
$ses _basket_items=1;
Fills the No. 0 position of 4 arrays, using the values passed from the href link.
Links are described in the ' Add links to your page ' section
$ses _basket_name[0]= $basket;
$ses _basket_amount[0]=1;
$ses _basket_price[0]= $price;
$ses _basket_id[0]= $id;
Register a new basket in the session
Session_register ("Ses_basket_items");
Session_register ("Ses_basket_name");
Session_register ("Ses_basket_amount");
Session_register ("Ses_basket_price");
Session_register ("ses_basket_id");
?>
This creates a basket, fills the value in the No. 0 position of the array, and registers the array with the session. A basket was born.
Fragment 4. Fill Basket
$basket _position_counter=0; The position in the basket
$double = 0; Dual entry flag set to No
if ($ses _basket_items>0) {
Check if the basket contains a double entry in the project
foreach ($ses _basket_name as $basket _item) {
Iterate through the names contained in the array and check to see if the matches from the href
if ($basket _item== $basket) {
If you already have an item in the basket, set flag to 1
$double = 1;
Remember the location of the project and update it
$basket _position= $basket _position_counter;
}
$basket _position_counter++; Increase the actual position in the basket
}
}
Update Basket
if ($double ==1) {
If the item already exists in your basket, update the number and location of the $basket_position processing
$oldamount = $ses _basket_amount[$basket _position];
$ses _basket_amount[$basket _position]++;
$amount = $ses _basket_amount[$basket _position];
$oldprice = $ses _basket_price[$basket _position];
Update Price
$newprice = ($oldprice/$oldamount) * $AMOUNT;
$ses _basket_price[$basket _position]= $newprice;
}else{
If it's not in your basket, add a new item at the end of the array
$ses _basket_name[]= $basket;
$ses _basket_amount[]=1;
$ses _basket_price[]= $price;
$ses _basket_id[]= $id;
$ses _basket_items++;
}
?>
Great, now you can fill the mini shopping basket and show it.
Organize code Snippets together
Let's organize the code together and save it as a minibasket.inc.
Remember that in code Snippet 1, the decision is not to increase?
Let's repeat it here.
if ($basket! = "") {
Here, the project will be added to the basket. Let's check if there's a registered basket.
if (session_is_registered ("Ses_basket_items")) {
There is a registered basket, put code snippet 4 here.
It adds items to the registered basket, checks for duplicate records, updates them or adds items at the end of the array
} else {
There is no registered basket, put code snippet 3 here. It creates a new basket, and
Register it through the session.
}
}
All that's left is code snippet 2. Used to display items in the basket if they are present.
Add to this.
?>
Look, it's good. "Cest tout," the French would say. If all is done, you can save this file as Minibasket.inc and include it in the page that displays the product's PHP (as the current mainstream development language).
Minibasket.inc and basket.php in zip format (as the current mainstream development language)
http://www.bkjia.com/PHPjc/509073.html www.bkjia.com true http://www.bkjia.com/PHPjc/509073.html techarticle fragment 3. Create a new basket and add a project to it? PHP (as the current mainstream development language)//Set the project count to 1 Ses_basket_items=1; Fills the No. 0 position of 4 arrays, ...