Mini shopping basket (2) _ PHP Tutorial implemented with PHP4Session only

Source: Internet
Author: User
Use only the PHP4Session to implement the mini shopping basket (2 ). Segment 3. Create a new basket and add a project to it? Php (as the mainstream development language) sets the project count to 1ses_basket_items1; fills in the 0th positions of the four arrays, Segment 3. Create a new basket and add a project to it


// Set the project count to 1
$ Ses_basket_items = 1;

// Fill in the 0th positions of the four arrays and use the values passed from the href link
// The link is described in 'Add a link to your page '.
$ 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 ");

?>

In this way, a basket will be created, so the value will be filled in the 0th position of the array, and the array will be registered using session. A basket was born.


Segment 4. Fill basket


$ Basket_position_counter = 0; // the position in the basket.
$ Double = 0; // double entry flag is set to NO
If ($ ses_basket_items> 0 ){
// Check whether the basket contains a double entry in the project
Foreach ($ ses_basket_name as $ basket_item ){
// Retrieve the name of the array and check whether it matches the name transmitted from href.
If ($ basket_item = $ basket ){
// If a project already exists in the basket, set flag to 1
$ Double = 1;
// Remember the project location and update it.
$ Basket_position = $ basket_position_counter;
}
$ Basket_position_counter ++; // add the actual position in the basket
}
}

// Update the basket
If ($ double = 1 ){
// If the project already exists in your basket, update the number and position of processing in $ basket_position.
$ Oldamount = $ ses_basket_amount [$ basket_position];
$ Ses_basket_amount [$ basket_position] ++;
$ Amount = $ ses_basket_amount [$ basket_position];
$ Oldprice = $ ses_basket_price [$ basket_position];
// Update the price
$ Newprice = ($ oldprice/$ oldamount) * $ amount;
$ Ses_basket_price [$ basket_position] = $ newprice;
} Else {
// If it is not in your basket, add a new project 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 display it.

Organize code snippets together

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


// Remember, in code snippet 1, decide whether to add?
// Let's repeat it here
If ($ basket! = ""){
// Here, the project will be added to the basket. Let's check whether a registered basket exists.
If (session_is_registered ("ses_basket_items ")){
// There is a registered basket where code snippet 4 is stored.
// It adds a project to the registered basket, checks duplicate records, updates them, or adds a project at the end of the array
} Else {
// No registered basket exists. put code snippet 3 here. It creates a new basket and
// Register it through session.
}
}
// The rest are code snippet 2. Displays items in a basket.
// Add it here.

?>

Look, good. "Cest tout", the French will say so. If all of them are done, you can save this file as minibasket. inc and include it in the php (as the mainstream development language) page that displays the product.

Minibasket. inc and basket. php in Zip format (as the mainstream development language)

Bytes. Create a new basket and add a project to it? Php (as the mainstream development language) // set the project count to 1 ses_basket_items = 1; // fill in the 0th position of the four arrays ,...

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.