PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Aug 15, 2020 3:28 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Calculator Script
PostPosted: Mon Feb 03, 2014 6:15 pm 
Offline
Forum Newbie

Joined: Mon Feb 03, 2014 6:02 pm
Posts: 1
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

Syntax: [ Download ] [ Hide ]
<?php # Script 3.10 - calculator.php #4

$page_title = 'Widget Cost Calculator';
include ('../includes/header.html');

// 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']) ) {
               
                // Print the heading
                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');

?>
 


Top
 Profile  
 
 Post subject: Re: Calculator Script
PostPosted: Mon Feb 03, 2014 6:39 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group