<?php
$date = date("l dS of F Y h:i:s A");
//echo date("l dS of F Y h:i:s A");
####################################################################
## The following code is an example of how to write ##
## the information given in the form to a text file - orders.txt. ##
####################################################################
## The $tireqty, $oilqty, and $sparkqty are passed from the form - orderform.html
$orderfile = 'orders.txt'; //file location - /wwwroot
$ordercontent = $date."\t".
$tireqty." tires \t".
$oilqty." oil \t".
$sparkqty." spark plugs \n";
print $total; //" total: \t".
print $address; //." address \n";
// Let's make sure the file exists and is writable first.
if (is_writable($orderfile)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($orderfile, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $ordercontent) === FALSE) {
echo "Cannot write to file ($orderfile)";
exit;
}
echo "Success, wrote ($ordercontent) to file ($orderfile)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
##########################################################
## This is the end of writing to a text file code example.
##########################################################
$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty;
##############################################################
## This if/else statement determines the quantitiy ##
##############################################################
if ($totalqty == 0) {
echo 'You did not order anything<br />';
}
else
{
echo "<p>Your order has been processed</p><br />";
echo "You ordered ".$_POST[tireqty] .' tires<br />';
echo "You ordered ".$_POST[oilqty].' bottles of oil<br />';
echo "You ordered ".$_POST[sparkqty] . 'spark plugs<br /><br />';
}
#########################################################################################
## The switch statment works similar way to the if statement, but allows the ##
## condition to take more than two values. In a if statement, the condition can ##
## be either true or false. ##
## In a switch statement, the condition can take any number of different values, ##
## as long as it evaluates to a simple type (integer, string, or double). You need ##
## to provide a case statement to handle each value you want to react to and, ##
## optionally, a default case to handle any that you do not provide a specific case ##
## statement for. ##
#########################################################################################
switch ($find)
{
case 'a' :
echo '<p>Regular customer.</p>';
break;
case 'b' :
echo '<p>Customer referred by TV advert</p>';
break;
case 'c' :
echo '<p>Customer referred by phone directory.</p>';
break;
case 'd' :
echo '<p>Customer referred by word of mouth.</p>';
break;
default :
echo '<p>No clue how they found us.</p>';
break;
}
#########################################################################################
## This if/elseif statement determines the discount of tires dependent on the quantity
## The customer must by more than 10 tires to get a discount - < 10. The other conditions
## depend on the amount purchased with then determines a discount rate. -
## ie) elseif ($tireqty > 100)
## $discount = 0.15;
## If a customer puts in an tire order of over 100, he/she gets a 15% discount
#########################################################################################
if ($tireqty < 10)
$discount = 0;
elseif ($tireqty > 10 && $tireqty <= 49)
$discount = 0.05;
elseif ($tireqty > 50 && $tireqty <=99)
$discount = 0.10;
elseif ($tireqty > 100)
$discount = 0.15;
echo 'Items ordered = ' .$totalqty.'<br /><br />';
$totalamount = 0.00;
define('TIREPRICE', 100);
define('OILPRICE', 10);
define('SPARKPRICE', 4);
$subtotal = $tireqty * TIREPRICE
+ $oilqty * OILPRICE
+ $spark * SPARKPRICE;
#####################################################################
## Since we indicated above that: ##
## if ($tireqty < 10) ##
## $discount = 0; ##
## then this below statment says, "if $discount is greater than 10 ##
##(that means the customer purchased more than 10 tires), echo ##
## 'You get a tire discount! ##
#####################################################################
$disrate = $subtotal * $discount;
$taxrate = 0.15;
if ($discount > 0) {
echo 'You get a tire discount!<br /><br />';
echo 'Discount Amount ' .number_format($disrate, 2).'</br></br>';
echo 'minus- '.number_format($disrate, 2 ).'<br /><br />';
$nsubtotal = $subtotal - $disrate;
echo 'New SubTotal '.number_format($nsubtotal, 2 ).'<br /><br />';
$totaltax = $nsubtotal * $taxrate;
echo 'Tax: $'.number_format($totaltax, 2).'<br />';
$total = $nsubtotal + $totaltax;
echo 'Total Including tax: $'.number_format($total, 2).'<br />';
}
else
{
echo 'SubTotal '.number_format($subtotal, 2 ).'<br /><br />';
$totaltax = $subtotal * $taxrate;
echo 'Tax: $'.number_format($totaltax, 2).'<br />';
$total = $subtotal + $totaltax;
echo 'Total Including tax: $'.number_format($total, 2).'<br />';
}
?>