Using PHP To Set Up Math Functions
I don’t think anything else that benefited from PHP then e-commerce. Without PHP I would think it be a lot tricker to run a e-commerce without a little PHP for back end support. Even though ASP is its own language and could do the same thing it is a bit tricky to run ASP on a linux server without doing some modifications.
So in this weeks script examples we will be learning to do some number crunching with the help of PHP and we will use a simple ordering form that is common when a person is about to purchase some products off a website.
On top of that we will be using PHP as an external document instead of internal and you will see why shortly. The first step in setting up our website and for php to make its calculation is a form and like any form you need to properly identify so that why PHP will be able to identify it and so it can run the script properly.
body {
font-family:"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif;
}
p, h1, form, button {
border:0;
margin:0;
padding:0;
}
.spacer {
clear:both;
height:1px;
}
/* ----------- My Form ----------- */
.myform {
margin:0 auto;
width:400px;
padding:14px;
}
/* ----------- stylized ----------- */
#stylized {
border:solid 2px #b7ddf2;
background:#ebf4fb;
}
#stylized h1 {
font-size:14px;
font-weight:bold;
margin-bottom:8px;
}
#stylized p {
font-size:11px;
color:#666666;
margin-bottom:20px;
border-bottom:solid 1px #b7ddf2;
padding-bottom:10px;
}
#stylized label {
display:block;
font-weight:bold;
text-align:right;
width:140px;
float:left;
}
#stylized .small {
color:#666666;
display:block;
font-size:11px;
font-weight:normal;
text-align:right;
width:140px;
}
#stylized input {
float:left;
font-size:12px;
padding:4px 2px;
border:solid 1px #aacfe4;
width:200px;
margin:2px 0 20px 10px;
}
#stylized select {
float:left;
font-size:12px;
padding:4px 0px;
border:solid 1px #aacfe4;
width:200px;
margin:2px 0 20px 10px;
}
#stylized button {
clear:both;
margin-left:150px;
width:125px;
height:31px;
background:#666666 text-align:center;
line-height:31px;
color:#FFFFFF;
font-size:11px;
font-weight:bold;
}
<div id="stylized" class="myform">
<form id="form" name="form">
<h1>Order Form</h1>
<p>Fill out this form to calculate the total cost:</p>
<label>Price <span class="small">Add Price of Product</span> </label>
<input type="text" name="price" size="5" />
<label>Quantity: <span class="small">How many do you Want?</span> </label>
<input type="text" name="quantity" size="5" />
<label>Discount: <span class="small">Discount Special</span> </label>
<input type="text" name="discount" size="5" />
<label>Shipping method: <span class="small">Min. size 6 chars</span> </label>
<select name="shipping">
<option value="5.00">Slow and steady</option>
<option value="8.95">Put a move on it.</option>
<option value="19.36">I need it yesterday!</option>
</select>
<label># of Payments: <span class="small">Number of payments </span> </label>
<input type="text" name="payments" size="5" />
<input type="submit" name="submit" value="Calculate!" />
<div class="spacer"></div>
</form>
</div>
Ok, we have constructed the form that we will be using and now we will be looking at the PHP next. I mention earlier that the php script that will be running this form is an external php script because of the fact as scripts get more complicated they get much larger. Which means the large the source code in the document the longer it will take to load and so by using a external php script your basically cutting the loading time down and on top of that the script will run the moment you click the calculate button.
So lets take a look at the code that our PHP file will be using and you will see how PHP sets up math functions.
/* This script takes values from calculator.html and performs total cost and monthly payment calculations. */ // Address error handling, if you want. // Get the values from the $_POST array: $price = $_POST['price']; $quantity = $_POST['quantity']; $discount = $_POST['discount']; $tax = $_POST['tax']; $shipping = $_POST['shipping']; $payments = $_POST['payments']; // Calculate the total: $total = $price * $quantity; $total = $total + $shipping; $total = $total - $discount; // Determine the tax rate: $taxrate = $tax/100; $taxrate = $taxrate + 1; // Factor in the tax rate: $total = $total * $taxrate; // Calculate the monthly payments: $monthly = $total / $payments; // Print out the results: print "<div>You have selected to purchase:<br /> <span class="number">$quantity</span> widget(s) at <br /> $<span class="number">$price</span> price each plus a <br /> $<span class="number">$shipping</span> shipping cost and a <br /> <span class="number">$tax</span> percent tax rate.<br /> After your $<span class="number">$discount</span> discount, the total cost is $<span class="number">$total</span>.<br /> Divided over <span class="number">$payments</span> monthly payments, that would be $<span class="number">$monthly</span> each.</p></div>";
As you see there is a lot going on in this script and if I were to put it in with the form the document will be come bit larger and although you won’t see the php when you do a view source code, it is still there running. In the first section “// Get the values from the $_POST array:”, we are telling the script to get the data that is being calculated by the math we set up within the PHP document and should be easy to understand as you read further.
The next part. “// Calculate the total:” is where we set up some basic math functions in order to make our calculations. We are telling our php script to provide three totals, the first one is the price of the object times how many of that object your getting, next your adding a shipping fee to the first total that has been made. ie at $4 we are getting 3 things and so that is $12 dollars and the shipping fee is $2. Therefore, we are going 12+2 to gives us a new total of 14. Now say that we have a discount and so you would have to subtract that from the current total and so an example would be 14-4 and so the overall total will be $10.
To sum up the PHP script is giving you three totals, first one is total amount of quantity of products, next your adding a shipping fee to give you a new total and then finally if there is a discount your subtracting from the second total to give you the final total of your product. Take a look at the tax payments and monthly payments and you should be able to figure out what is happening there. So by the time all that math is done you should have an idea how many your paying and how many times you will be paying it.
Of course we still have to display all those totals and so we set up a simple print command to display the data that will be posted once the math is completed. So now make sure you have save the files correctly, upload them to your server and throw in some numbers and watch the calculator at work.













Leave your response!
You must be logged in to post a comment.