Home > Backend Development > PHP Tutorial > Mini shopping basket implemented using only PHP4 Session (2)_PHP tutorial

Mini shopping basket implemented using only PHP4 Session (2)_PHP tutorial

WBOY
Release: 2016-07-13 17:31:30
Original
905 people have browsed it

Fragment 3. Create a new basket and add an item to it

(as the current mainstream development language)

//Set the item count to 1
$ses_basket_items=1;

// Fill the 0th position of the 4 arrays, using the value passed from the href link
// The link is in the 'Add a link to your page' section What we said
$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 will create a basket, fill all the values ​​in the 0th position of the array, and Use session to register array. A basket was born.


Clip 4. Fill the basket

(as the current mainstream development language)

$basket_position_counter=0; //Position in the basket
$double= 0; //Double entry flag is set to NO
if ($ses_basket_items>0){
// Check whether the items contained in the basket have dual entries
foreach ($ses_basket_name as $basket_item){
// Traverse the names contained in the array and check whether they match the ones passed from href
if ($basket_item==$basket){
// If there is already an item in the basket, add Flag is set to 1
$double=1;
// Remember the position of the item and will update it
$basket_position=$basket_position_counter;
}
$basket_position_counter++; //Add to the basket The actual position of The quantity and position processed by $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 price
$newprice=($oldprice/$oldamount)*$amount;
$ses_basket_price[$basket_position]=$newprice;
}else {
// If it is not in your basket, add 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 display it.

Group the code snippets together

Let’s group the code together and save it as minibasket.inc.

(as the current mainstream development language)


// Remember in code snippet 1, decide whether to add?
// Let’s repeat it here
if ($basket!=""){ // Here, the item 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 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, replace code snippet 3 Set it here. It creates a new basket and
// registers it with the session.
}
}
// The rest is code snippet 2. Used to display items in the basket if they are present.
//Add here.

?>

Look, not bad. "cest tout", the French would say. If everything is done, you can save this file as minibasket.inc and include it in the php
(as the current mainstream development language)
page that displays the product.

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

TechArticleFragment 3. Create a new basket and add an item to it?php (as the current mainstream development language) // Set the item count to 1 ses_basket_items=1; // Fill the 0th position of the 4 arrays,...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template