## Calculator Script

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

peterlytham
Forum Newbie
Posts: 1
Joined: Mon Feb 03, 2014 5:02 pm

### Calculator Script

I'm new to this programing lark and I was wondering if someone could help.
I have created a script that calculates widgets. You have to enter a value and amount and the script displays the results.

Lets assume that we wanted the user to select from dropdown boxes. I.E. We have 3 types of widgets (Small, medium and large) Small widgets are say £ 50.00, medium are £100.00 and large are £ 150.00. You can only order these widgets in multiples of ten, say upto 50 max. E.G 10, 20, 30, 40, 50. (Hope I have explained that right).

I know how to create dropdown boxes with select values but not sure how to change this script to incorporate them.

Any help would be apreciated

Peter

Code: Select all

``````<?php # Script 3.10 - calculator.php #4

\$page_title = 'Widget Cost Calculator';

// This function calculates the total and prints the results. We have made the \$tax argument optional for the user. It now has a default value.

function calculate_total (\$qty, \$cost, \$tax = 5) {

\$total = (\$qty * \$cost);
\$taxrate = (\$tax / 100); // Turns 5% into 05.
\$total += (\$total * \$taxrate); // Add the tax

return number_format(\$total, 2);

} // end of function

// Check the form submission
if (isset(\$_POST['submitted'])) {

// Minimal form submission
if ( is_numeric(\$_POST['quantity']) && is_numeric(\$_POST['price']) ) {

echo '<h1>Total Cost</h1>';

// Call the function with or without tax:

if (is_numeric(\$_POST['tax'])) {
\$sum = calculate_total (\$_POST['quantity'], \$_POST['price'], \$_POST['tax']);
} else {
\$sum = calculate_total (\$_POST['quantity'], \$_POST['price']);
}

// print the results:
echo '<p>The total cost of purchasing ' . \$_POST['quantity'] . ' Widget(s) at £' . number_format (\$_POST['price'], 2) . ' each, with tax, is £' . \$sum . '.</p>';

} else { // invalid submitted values:
echo '<h1>Error!</h1>
<p class="error">Please enter a valid quantity and price.</p>';
}

} // end of main isset() IF.

// Leave the PHP section and create the HTML form:
?>

<h1>Widget Cost Calculator</h1>

<form action="calculator4.php" method="post">

<p>Quantity: <input type="text" name="quantity" size="5" maxlengh="5" value="<?php if(isset(\$_POST['quantity'])) echo \$_POST['quantity']; ?>" /></p>
<p>Price: <input type="text" name="price" size="5" maxlengh="10" value="<?php if(isset(\$_POST['price'])) echo \$_POST['price']; ?>" /></p>

<p>Tax (%): <input type="text" name="tax" size="5" maxlengh="5" value="<?php if(isset(\$_POST['tax'])) echo \$_POST['tax']; ?>" />(Optional)</p>

<p><input type="submit" name="submit" value="Calculate" /></p>

<input type="hidden" name="submitted" value="TRUE" />

</form>

<?php // include the footer:

include ('../includes/footer.html');

?>
``````
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

### Re: Calculator Script

I take it you know how to do their HTML? Not sure what else you might mean...

That's a good first step. How about sticking it into the form you already have there? Don't mind the PHP for a moment.