Home  >  Article  >  Backend Development  >  Mini shopping basket implemented by PHP4 Session 5_PHP tutorial

Mini shopping basket implemented by PHP4 Session 5_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:28:441177browse

?. Fill basket
$#@60;?php
$basket_position_counter=0; //Position in basket
$double=0; //Double entry flag set to NO
if ($ses_basket_items$ #@62;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 from Matches passed by href
if ($basket_item==$basket){
// If there is already an item in the basket, set the flag to 1
$double=1;
// Remember the position of the item, it will be updated
$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 quantity and position processed 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 price
$newprice=($oldprice/$oldamount)*$amount;
$ses_basket_price[$basket_position]=$newprice;
}else{
// If it’s not in your basket, it’s in the array Add new items at the end
$ses_basket_name[]=$basket;


$ses_basket_amount[]=1;
$ses_basket_price[]=$price;
$ses_basket_id[]=$id ;
$ses_basket_items++;
}
?$#@62;
Great, now you can fill the mini basket and display it.
Group the code snippets together
Let’s group the code together and save it as minibasket.inc.
$#@60;?php
// 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 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.
?$#@62;
Look, not bad. "cest tout", the French would say. If you search for inibasket.inc and include it in the PHP page that displays the product.
minibasket.inc and basket.php in Zip format

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531762.htmlTechArticle?. Fill basket $#@60;?php $basket_position_counter=0; //Position in basket $double=0; //Double entry flag set to NO if ($ses_basket_items$#@62;0){ // Check if basket Contains items...
Statement:
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