Larry Ullman's Book Forums

# Chapter 3: Handle_Calc.php

## Recommended Posts

Dang it! I've already hit yet another snag!

This is my current code for the Product Cost Calculator ("handle_calc.php" p.84&85):

<!doctype html>

<html lang="en">
<meta charset="utf-8">
<title>Product Cost Calculator</title>
<style type="text/css">
.number { font-weight: bold; }
</style>
<body>
<?php // Script 4.2 - handle_calc.php
/* 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;

// Apply the proper formatting:
\$total = number_format(\$total, 2);
\$monthly = number_format(\$monthly, 2);

// Print out the results:
print "<p>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>";

?>
</body>
</html>

This the result when I run the calculator ("calculator.html"):

You have selected to purchase:
4 widget(s) at
\$99 price each plus a
\$8.95 shipping cost and a
5.5 percent tax rate.
After your \$25 discount, the total cost is \$400.85.
Divided over 24 monthly payments, that would be \$ each.

I'm looking, but I don't know why that last figure ("16.70") is not appearing.

Edited by hfcnew
##### Share on other sites

Considering the last line:

Divided over <span class=\"number\">\$payments</span> monthly payments, that would be \$<span class=\"number\"\$monthly</span> each.</p>";

That would now be: <span class=\"number\">\$monthly</span>

All the best!

Tri.

##### Share on other sites

Considering the last line:

Divided over <span class=\"number\">\$payments</span> monthly payments, that would be \$<span class=\"number\"\$monthly</span> each.</p>";

That would now be: <span class=\"number\">\$monthly</span>

All the best!

Tri.

Oh geez!

Thanks!!

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×

• #### Activity

×
• Create New...