PHPBuilder - Mini Shopping Basket with only PHP4 Sessions Page 4



RSS Twitter
Articles Application Architecture

Mini Shopping Basket with only PHP4 Sessions - Page 4

by: Evert Smith
|
August 17, 2000

Snippet 3: Starting a new basket and add 1 item to it

<?php

// Put the item counter to 1
$ses_basket_items=1;

// Fill the 4 arrays at position 0 with the values from the href link
// shown in the 'adding the link to your page' part
$ses_basket_name[0]=$basket;
$ses_basket_amount[0]=1;
$ses_basket_price[0]=$price;
$ses_basket_id[0]=$id;

// register the 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 start the basket, fill all values at position 0 of the arrays and register the arrays with the session. A basket is born!
Snippet 4. Filling the basket

<?php

$basket_position_counter
=0//Position in basket
$double=0//Flag for double entries set to NO
if ($ses_basket_items>0){
     
// If the basket contains items check for double entries
     
foreach ($ses_basket_name as $basket_item){
            
// Run through the array that contains the name and
            // check if it matches the item from the href.
            
if ($basket_item==$basket){
                 
// Set Flag to 1 if the item is already in the basket.
                 
$double=1;
                 
// Remember the position of the item to be updated
                 
$basket_position=$basket_position_counter;
            }
            
$basket_position_counter++; //Increment actual Position in basket
     
}
}

// Update the basket        
if ($double==1){
     
// Update amount and price at position $basket_position
     // if the item is already in your basket
     
$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{
     
// Add new item at end of array
     // if it is not in your basket
     
$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 minibasket and display it.

« Previous Page
1
|
2
|
3
|
4
|
5
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Evert Smith

Comment:



Comment:

(Maximum characters: 1200). You have characters left.