Developing Webs logo
Google
Web DW-Main DW-Photoshop
Visit the message board for solutions or post a question Check out the Classes offered for free at Developing Webs Voice Chat submit a tutorial, resources, article, font for publication consideration and earn a one way link back to your site Get more specifics about the Developing Webs Voice chat room and its room members View the current news at Developing Webs alternative sites which are resources for your d3evelopment needs Join Developing Webs Chat Link to us on your web page to help support this resource! Check out Developing Webs At yahoo groups! Developing Webs Home Adobe Photoshop Tutorials Macromedia Flash Tutorials Jasc Paint Shop Pro Tutorials Adobe Illustrator Animation basics Some Basic PHP tutorials Perl Tutorials HTML Tutorials Cascading Style Sheets javascript Front Page Tutorials Macromedia Dream Weaver Tutorials dreamweaver Publishing your site, FTP, ChMod, Promotions Tools to help you create your site Free and shareware fonts to download Photographs to use in your projects Shop for your site needs Free Online classes REcommend this site to others Log in or edit your information when logged in Developingwebs Log Out Change My Account details and preferences
RSS Newsfeed
for your site
DW News
Calendar
DW Forum
Right click on button and copy shortcut

Add to My Yahoo!

Reminder
Remember to book mark this site!



Request a Tutorial

Form Calculations

In this tutorial you will be learning the following:

  • assigning variables
  • operators
  • outputting info

You should have completed these tutorials:  PHP Form and Order Processing in this series.

1. Open up your processingorder.php. We need to first assign your variables a value that will set the variable type. Set the number value set of $totalqty to a whole number, and set the number value set of $totalamount to a real number by typing the followingJust above the end of the script tag (?>):

$totalqty = 0;
$totalamount = 0.00;

Set the initial values for the price of tires, oil and sparkplugs. The constants are always typed in all capitals by typing in the following:

define("TIREPRICE", 100);
define("OILPRICE", 10);
define("SPARKPLUGPRICE", 4);

2. Inside your PHP script just above the ?> we want to add our calculations. First  set $totalqty equal to the sum of each of the quantities, that is, $totalqty = $tireqty + $oilqty + $sparkplugqty; .

<?
echo "<P>Order Processed.";
echo date("H:i, jS F");
echo "<br>";
echo $tireqty." tires<BR>";
echo $oilqty." bottles of oil<BR>";
echo $sparkplugqty." spark plugs<BR>";

$totalqty = 0;
$totalamount = 0.00;

define("TIREPRICE", 100);
define("OILPRICE", 10);
define("SPARKPLUGPRICE", 4);

$totalqty = $tireqty + $oilqty + $sparkplugqty;
?>

3. Knowing the quantities of each item, we can multiply them by a SET price or CONSTANT. All constants are written without the $ and are written in capital letters. The left side of the equation is set equal to the right side, in this case, the right side will calculate the price for each item, tires, oil, and sparkplugs and add them together. It can be calculated by the following

Total price = (tire quantity X tire price) + (oil quantity X oil price) + (sparkplug quantity X SPprice)

To write this in PHP we have to use PHP formatting with the variables and constant(prices) we set. We can rely on PHP to always do all mutiplication and division pior to addition so we drop quotes. The multiplication symbol is an *  and addition +. So we place the following:

$totalamount = $tireqty * TIREPRICE
                        + $oilqty * OILPRICE
                        + $sparkplugqty * SPARKPLUGPRICE;

 

In our order processing script page our script should now look like this:

<?
echo "<P>Order Processed.";
echo date("H:i, jS F");
echo "<br>";
echo $tireqty." tires<BR>";
echo $oilqty." bottles of oil<BR>";
echo $sparkplugqty." spark plugs<BR>";

$totalqty = 0;
$totalamount = 0.00;

define("TIREPRICE", 100);
define("OILPRICE", 10);
define("SPARKPLUGPRICE", 4);


$totalqty = $tireqty + $oilqty + $sparkplugqty;
$totalamount = $tireqty * TIREPRICE
                        + $oilqty * OILPRICE
                        + $sparkplugqty * SPARKPLUGPRICE;

?>

4. We need to format the number that will be calculated so that it is in 2 decimal places, representing cents. To do this we can take the newly calculated $totalamount and apply a number_format command set at 2 decimal places. 

<?
echo "<P>Order Processed.";
echo date("H:i, jS F");
echo "<br>";
echo $tireqty." tires<BR>";
echo $oilqty." bottles of oil<BR>";
echo $sparkplugqty." spark plugs<BR>";

$totalqty = 0;
$totalamount = 0.00;

define("TIREPRICE", 100);
define("OILPRICE", 10);
define("SPARKPLUGPRICE", 4);


$totalqty = $tireqty + $oilqty + $sparkplugqty;
$totalamount = $tireqty * TIREPRICE
                        + $oilqty * OILPRICE
                        + $sparkplugqty * SPARKPLUGPRICE;
$totalamount = number_format($totalamount, 2);
?>

To those not used to programming, they may wonder why we can set $totalamount to $totalamout. Remember, that the variable on the left is being reset by the calculation on the right side.

5. Outputting this data to your order processing page that the user will see involves the user of the echo command (see the second tutorial here.) 

First add a line break: 

echo "<BR>\n";

Add a line that gives you the total number of items ordered. First label what it is, Items ordered, then use the period to concatenate the variable after it. Follow this with a line break.

echo "Items ordered:                 ".$totalqty."<br>\n;

Add a line that gives you the total cost of the order, and space the total cost so the items line up in a browser. Then add a period to concatenate the variable value, followed by a long break.

echo "Subtotal:                         ".$totalamount."<BR>"\n;

Define the tax rate at 10% by setting the variable, no echo here. 10 % is equal to 10 cents on a dollar, so the rate will be 0.10. 

$taxrate = 0.10;    // local sales tax is 10% here

Calculate the total amount as 110%(1+the tax rate) of the previously calculated varable. This will be your total including the 10%tax.

 $totalamount = $totalamount * (1+$taxrate);
 

You will then need to format the text to 2 decimal places.

$totalamount = number_format($totalamount, 2);
 

You will then need to give the total including tax.

echo "total Including Tax: $".$totalamount."<BR>\n";

6. So far your total script page looks like this:

 

<?
echo "<P>Order Processed.";
echo date("H:i, jS F");
echo "<br>";
echo $tireqty." tires<BR>";
echo $oilqty." bottles of oil<BR>";
echo $sparkplugqty." spark plugs<BR>";

$totalqty = 0;
$totalamount = 0.00;

define("TIREPRICE", 100);
define("OILPRICE", 10);
define("SPARKPLUGPRICE", 4);


$totalqty = $tireqty + $oilqty + $sparkplugqty;
$totalamount = $tireqty * TIREPRICE
                        + $oilqty * OILPRICE
                        + $sparkplugqty * SPARKPLUGPRICE;
$totalamount = number_format($totalamount, 2);
echo "<BR>\n";
echo "Items ordered:                 ".$totalqty."<br>\n;
echo "Subtotal:                         ".$totalamount."<BR>"\n;
$taxrate = 0.10;    // local sales tax is 10% here
$totalamount = $totalamount * (1+$taxrate);
$totalamount = number_format($totalamount, 2);
echo "total Including Tax: $".$totalamount."<BR>\n";
?>

 



"Building The Web Into a Nicer Place -- One Site At A Time"
Developing Webs Group Copyright © 2001-2008 All Rights Reserved
Privacy and Legal