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

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) /**********************************************/ { ?>
sort = $sort; echo "
"; echo "
\n"; echo "User Name
\n"; echo "\n"; $this->display_basket($_GET['action']); } else { print "The basket is empty"; ?>
display_basket($_GET['action']); $items = addslashes($values[0]); $total = $values[1]; $db = mysql_connect ($dbhost, $dbuser,$password) or die("Database connection failed"); mysql_select_db ('canardsc_canards',$db); if (!$user) { echo "Enter User Name!\n"; } else { $word = $_POST['user']; $user = mysql_escape_string($word); $date = time(); $date = localtime($date); $year = $date[5]+1900; $month = $date[4]+1; $day = $date[3]; $hour = $date[2]; $minute = $date[1]; $second = $date[0]; $date = $year."/".$month."/".$day."/ ".$hour.":".$minute.":".$second; $query = "INSERT INTO $this->table2 " . "VALUES('','$date', '$user','$items','$total')"; mysql_query($query) or die("failure"); echo "Thank you!
\n";?> Return

display=$display; $this->subject=$subject; if (!$display) { $display = 'all'; $subject = 'all'; } $this->sort = $sort; if (!$sort) { $sort = 'basket'; } ?>
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"; } ?>


  • 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(); } ?>