Help with some basic code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
t_pinchen
Forum Newbie
Posts: 6
Joined: Mon Feb 04, 2008 4:03 pm

Help with some basic code

Post by t_pinchen »

Hi,

I apologize if my use of the forums tags is improper I am incredibly new to all web programming and so may have got it wrong.

I was just wondering if anybody could help me with my code which is returning the error below. The code is supposed to create a simple calculator to format quantity, price and tax into a grand total.

Any help given would be greatly appreciated.
Many thanks
Tom :)

Code: Select all

 
<?php # Script 3.5 - calculator.php
$page_title = 'Widget Cost Calculator';
 
function calculate_total ($qty, $cost, $tax = 17.5) {
 
$taxrate = $tax / 100; // Turn 17.5% into .175.
$total = ($qty * $cost) * ($taxrate + 1);
return number_format ($total, 2)
}
 
if (isset($_POST['submitted'])) {
 
if (is_numeric($_POST['quantity']) &&
is_numeric($_POST['price'])) {
 
echo '<h1 id="mainhead">Total Cost</h1>';
 
if (is_numeric($_POST['tax'])) {
$total_cost = calculate_total
($_POST['quantity'], $_POST['price'], $_POST['tax']);
} else {
$total_cost = calculate_total
($_POST['quantity'], $_POST['price']]);
}
 
echo '<p>The total cost of 
purchasing ' . $_POST['quantity']
. ' widget(s) at $' . number_format
($_POST['price'], 2) . 'each is $'
. $total_cost . '.</p>
 
echo '<p><br /><p>';
 
} else { 
echo '<h1 id="mainhead">Error!</h1>
<p class="error">Please enter a
valid quantity and price.</p><p>
<br /></p>';
}
}
?>
 
<h2>Widget Cost Calculator</h2>
<form_action="calculator.php"
method="post">
 
<p>Quantity: <input type="text"
name="quantity" size="5" maxlength="10"
value="<?php if (isset($_POST
['quantity'])) echo $_POST['quantity'];
?>"/></p>
 
<p>Price: <input type="text"
name="price" size="5" maxlength="10"
value="<?php if (isset($_POST
['price'])) echo $_POST['price'];
?>"/></p>
 
<p>Tax (%): <input type="text"
name="tax" size="5" maxlength="10"
value="<?php is (isset($_POST
['tax']; ?>" /></p>
 
<p><input type="submit" name="submit"
value="Calculate!"/></p>
 
<input type="hidden" name="submitted"
value="TRUE" />
 
</form>
<?php
 
 
 

Code: Select all

 
Parse error: syntax error, unexpected '}' in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\calculator.php on line 17
 
Last edited by t_pinchen on Mon Feb 04, 2008 4:21 pm, edited 2 times in total.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Help with some basic code

Post by califdon »

Match up your beginning and ending braces. Somewhere among your if statements, you have one too many closing braces.

For posting in this forum, use [syntax=php]and[/syntax] tags to enclose PHP code, you can use [syntax=php]and[/syntax] for other things.
t_pinchen
Forum Newbie
Posts: 6
Joined: Mon Feb 04, 2008 4:03 pm

Re: Help with some basic code

Post by t_pinchen »

Hey,

Thanks for your quick reply and advice, I have managed to fix my problem :D

Thanks again,
Tom
Post Reply