Home > Article > Backend Development > Mini shopping basket implemented by PHP4 Session 5_PHP tutorial
?. 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;