class basket
{
var $table1;
var $table2;
var $displaydiv;
var $basketdiv;
function show_default()
{
if (!$action)
if (!$id)
{
session_destroy();
}
}
function select_item($id)
{
if ($id)
{
$db = mysql_connect ($dbhost, $dbuser,$password)
or die("Database connection failed");
mysql_select_db ('canardsc_canards',$db);
$query = "SELECT * FROM $this->table1 WHERE id=$id";
$result = mysql_query ($query);
$row = mysql_fetch_assoc($result);
$basket = $row['basket'];
$price = $row['price'];
}
// remember the code from Snipplet 1, deciding whether to add or not?
// Let's repeat it here
if ($basket!=""){
//In here items will be added to the basket. let's check if there is already
//a basket registered.
if (isset($_SESSION['ses_basket_items'])){
// There is a registered basket
// Place Snipplet 4 here. It adds the item to the registered basket, checks
// for duplicate entries, updates them or adds the item at the end of the
// arrays.
$basket_position_counter=0; //Position in basket
$double=0; //Flag for double entries set to NO
if ($_SESSION['ses_basket_items']>0){
// If the basket contains items check for double entries
foreach ($_SESSION['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){
$basket_amount = $_SESSION['ses_basket_amount'];
$basket_price = $_SESSION['ses_basket_price'];
$basket_name = $_SESSION['ses_basket_name'];
$basket_id= $_SESSION['ses_basket_id'];
/*************************************************************/
// Update amount and price at position $basket_position
// if the item is already in your basket
$oldamount= $basket_amount[$basket_position];
$basket_amount[$basket_position]++;
$amount= $basket_amount[$basket_position];
$oldprice= $basket_price[$basket_position];
//update the price
$newprice=($oldprice/$oldamount)*$amount;
$basket_price[$basket_position]=$newprice;
$_SESSION['ses_basket_amount'] = $basket_amount;
$_SESSION['ses_basket_name']= $basket_name;
$_SESSION['ses_basket_price'] = $basket_price;
$_SESSION['ses_basket_id'] = $basket_id;
}else{
// Add new item at end of array
// if it is not in your basket
$total = array_push($_SESSION['ses_basket_name'],$basket);
$total = array_push($_SESSION['ses_basket_amount'], 1);
$total = array_push($_SESSION['ses_basket_price'],$price);
$total = array_push($_SESSION['ses_basket_id'],$id);
$_SESSION['ses_basket_items']++;
}
} else {
// There is no basket registered
// Place Snipplet 3 here. It creates a new basket and registers it with
// the session.
// Put the item counter to 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
$_SESSION['ses_basket_name'] = array();
$_SESSION['ses_basket_amount'] = array();
$_SESSION['ses_basket_price'] = array();
$_SESSION['ses_basket_id'] = array();
array_push($_SESSION['ses_basket_name'],$basket);
array_push($_SESSION['ses_basket_amount'], 1);
array_push($_SESSION['ses_basket_price'],$price);
array_push($_SESSION['ses_basket_id'],$id);
$_SESSION['ses_basket_items']++;
}
}
if ($_SESSION['ses_basket_items']>0 && $action!="destroy" && $action!="delete")
{
$this->display_basket($_GET['action']);
}
return $_SESSION;
}
function display_basket()
{
if ($_SESSION['ses_basket_items']>0)
{
?>
Basket
// If there are items in your basket
for ($basket_counter=0;$basket_counter<$_SESSION['ses_basket_items'];$basket_counter++){
// Go through the basket and print out each line
// You can of course format this to be in a table
// The formatting is needed to display the cents of your order. .00 will not display
// if not formatted .
$basket_amount = $_SESSION['ses_basket_amount'];
$basket_price = $_SESSION['ses_basket_price'];
$basket_name = $_SESSION['ses_basket_name'];
$price=sprintf("%01.2f",$basket_price[$basket_counter]);
$amount=$basket_amount[$basket_counter];
$name=$basket_name[$basket_counter];
$total = $total + $basket_price[$basket_counter];
if ($amount > 0 && $action != "delete")
{
echo "$amount $name $price"." $"."\n";
$items=$items."\n".$amount." ".$name." ".$price;
}
/*****************************************************************/
}
echo " |
TOTAL " . $total." $"." |
\n";
}
elseif ($action != "delete") {
// there are no articles in your basket
// setting the item counter to 0 and unsetting all variables
// This is a clean up here. It prevents people from getting old arrays back.
$_SESSION['ses_basket_items']=0;
unset($_SESSION['ses_basket_name']);
unset($_SESSION['ses_basket_amount']);
unset($_SESSION['ses_basket_price']);
unset($_SESSION['ses_basket_id']);
}
$values = array($items, $total);
return $values;
}
function delete_item($id)
{
if ($_SESSION['ses_basket_items'] > 0)
{
$basket_position_counter=0;
$basket_amount = $_SESSION['ses_basket_amount'];
$basket_price = $_SESSION['ses_basket_price'];
$basket_name = $_SESSION['ses_basket_name'];
$basket_id= $_SESSION['ses_basket_id'];
{
foreach ($basket_id as $basket_it){
// Run through the array that contains the name and
// check if it matches the item from the href.
if ($basket_it==$id){
if( $basket_amount[$basket_position_counter] > 1)
{
$oldamount = $basket_amount[$basket_position_counter];
$oldprice = $basket_price[$basket_position_counter];
$basket_amount[$basket_position_counter]--;
$basket_price[$basket_position_counter] = $oldprice * $basket_amount[$basket_position_counter] /$oldamount;
} else {
$basket_price[$basket_position_counter]="";
$basket_id[$basket_position_counter]=0;
$basket_name[$basket_position_counter]="";
$basket_amount[$basket_position_counter] = 0;
}
$_SESSION['ses_basket_amount'] = $basket_amount;
$_SESSION['ses_basket_name']= $basket_name;
$_SESSION['ses_basket_price'] = $basket_price;
$_SESSION['ses_basket_id'] = $basket_id;
}
$basket_position_counter++;
}
}
}
if ($_SESSION['ses_basket_items']>0 && $action!="destroy")
{
$this->display_basket($_GET['action']);
}
}
function purchase_form($sort)
/**********************************************/
{
?>
if (isset($_SESSION['ses_basket_name']))
{
$this->sort = $sort;
echo "
";
echo "
}
}
function submit($user)
{
?>
}
function display($sort, $display, $subject)
{
$this->display=$display;
$this->subject=$subject;
if (!$display)
{
$display = 'all';
$subject = 'all';
}
$this->sort = $sort;
if (!$sort)
{
$sort = 'basket';
}
?>
$db = mysql_connect ($dbhost, $dbuser,$password)
or die("Database connection failed");
mysql_select_db ('canardsc_canards',$db);
if ($display != 'all')
{
if ($display != 'year')
{
$query = "SELECT * FROM $this->table1 WHERE $display = '$subject' ORDER BY $sort";
}
else {
$query = "SELECT * FROM $this->table1 WHERE $display LIKE '$subject' ORDER BY $sort";
}
}
else {
$query = "SELECT * FROM $this->table1 ORDER BY $sort";
}
$result = mysql_query ($query);
mysql_close();
while ($row = mysql_fetch_assoc ($result)) {
$basket = htmlentities ($row['basket']);
$id = $row['ID'];
$price = $row['price'];
$director = htmlentities($row['director']);
$genre = htmlentities($row['genre']);
$year = $row['year'];
print"
Add $basket: $year
DIR: $director $price $Delete\n";
}
?>
}
}
$b = new basket();
$b->table1 = $table1;
$b->table2 = $table2;
$b->displaydiv = $displaydiv;
$b->displaydiv2 = $displaydiv2;
$b->displaydiv3 = $displaydiv3;
$b->basketdiv = $basketdiv;
$b->formdiv = $formdiv;
$b->display($_GET['sort'], $_GET['display'],$_GET['subject']);
switch($_GET['action']) {
case 'select';
$b->select_item($_GET['id']);
break;
case 'delete';
$b->delete_item($_GET['id']);
break;
case 'submit';
$b->submit($_POST['user']);
break;
case 'purchase';
$b->purchase_form($_GET['sort']);
break;
default:
$b->show_default();
}
?>