Mini Shopping baskets implemented with PHP4 session only (II)

Source: Internet
Author: User
Tags array
Session Fragment 3. Create a new basket and add a project to it

<?php

Set Item count to 1
$ses _basket_items=1;

Fills the No. 0 position of the 4 array, using the values passed from the href link.
The link is 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 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

<?php

$basket _position_counter=0; The position in the basket
$double = 0; Double entry flag set to No
if ($ses _basket_items>0) {
Check if the basket contains a double entry in the item
foreach ($ses _basket_name as $basket _item) {
Iterate through the names contained in the array to check if the match came from the href
if ($basket _item== $basket) {
If you already have a project in the basket, set the 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) {
Update the number and location of $basket_position processing if the item already exists in your basket
$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 in the mini shopping basket and show it.

To organize snippets of code together

Let's organize the code together and save it as a minibasket.inc.

<?php

Remember, in code fragment 1, is the decision to be increased?
Let's repeat it here
if ($basket!= "") {
Here, the project will be added to the basket. Let's check if there is a registered basket.
if (session_is_registered ("Ses_basket_items")) {
There is a registered basket, put the code fragment 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 the code fragment 3 here. It creates a new basket, and
Register it by session.
}
}
All that's left is code fragment 2. If there are items in the basket, use them to display them.
Add to this.

?>

Look, it's good. "C ' est tout," the Frenchman would say. If all is done, you can save this file for Minibasket.inc
and include it in the PHP page that displays the product.

Minibasket.inc and basket.php in zip format


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.