Mini shopping basket implemented only with PHP4Session (2)_PHP tutorial

WBOY
Release: 2016-07-13 17:21:56
Original
691 people have browsed it

Fragment 3. Create a new basket and add an item to it
// Set the item count to 1
$ses_basket_items=1;
// Fill the 0th position of the 4 arrays, Use the value passed from the href link
// The link is as described in the 'Add a link 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 will create a basket , fill in all values ​​in the 0th position of the array, and use session to register the array. A basket was born.
Clip 4. Fill basket
$basket_position_counter=0; //Position in basket
$double=0; //Double entry flag set to NO
if ($ses_basket_items>0){
// Check whether there are double entries in the items contained in the basket
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, set flag to 1


$double=1;
// Remember The position of the item will be updated
$basket_position=$basket_position_counter;
}
$basket_position_counter++; //Increase the actual position in the basket
}
}
//Update the basket
if ($double==1){
// If the item already exists in your basket update the amount and position processed at $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 a new item to 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 basket and display it.
Putting the code snippets together
Let’s put the code together and save it as minibasket.inc.
// Remember in code snippet 1, decide whether to increase?
// 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 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 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 all is done, you can save this file as minibasket.inc and include it in the PHP page that displays the products.
minibasket.inc and basket.php in Zip format

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532375.htmlTechArticleFragment 3. Create a new basket and add an item to it // Set the item count to 1 $ses_basket_items=1; // Fill the 0th position of the 4 arrays with the value passed from the href link /...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!