Mini Shopping basket with Php4session only (ii) _php tutorial

Source: Internet
Author: User
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 PHP page where the product is displayed.
Minibasket.inc and basket.php in zip format

http://www.bkjia.com/PHPjc/532375.html www.bkjia.com true http://www.bkjia.com/PHPjc/532375.html techarticle fragment 3. Create a new basket and add a project to it//set the project count to 1 $ses _basket_items=1; Fills the No. 0 position of 4 arrays, using the values passed from the href link/...

  • 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.