Jump to content

Visual QS: PHP 6 and MySQL 5 - script won't work


trixiesirisheyes

Recommended Posts

I'm still learning about this stuff. I got the initial script to work, but then the book said, under Tips, page 105, that I could exchange my initial return statement for return array($total, $tax);

 

When I do, it returns a statement on the web page that says $Array.

 

Here's my code:

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

 

$page_title = 'Widget Cost Calculator';

include ('includes/header.html');

 

/* This function calculates a total

and then prints the results

The $tax argument is optional (it has a default value). */

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

 

$total = ($qty * $cost);

$taxrate = ($tax / 100); //Turn 5% into .05

$total += ($total * $taxrate); //Add the tax

 

return array($total, $tax);

 

} //End of function

 

//Check for form submission:

if(isset($_POST['submitted'])) {

 

//Minimal form validation

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="calculator.php" method="post">

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

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

    <p>Tax (%): <input type="text" name="tax" size="5" maxlength="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 footer

include ('includes/footer.html');

?>

 

 

And here's where it can be seen:

 

http://mdh-test.com/php_training/calculator.php

 

Fill out the form and see what I mean. What am I doing wrong?

 

Thanks in advance for any help you can give me!

The book had me do the return statement exactly as I put it there, with no mention of more syntax issues.

 

Based on your explanation, I still don't understand. Sorry. I have no idea how I would code this, since I know I wouldn't put the statement "$sum[0] will be the total and $sum[1] will be the tax" into the return array(); , and "sum" isn't even mentioned until further down in the script. What am I not understanding?

 

This book has had other issues where it doesn't explain something very well.

The exact php version is secondary to the issue of returning an array and using the elements of the array in the code.

 

Your posted code has the following and it is probably where you are getting the word "Array" output where the variable $sum is being used -

   //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>';

 

When you changed the function definition so that it returns an array instead of a single value, $sum is now an array instead of a one value. To echo the elements of the array $sum, you must use array syntax, $sum[0] and $sum[1] instead of just $sum

 

I'm in the beginning of the book, so I don't think anything is so complicated as to make the PHP version an issue yet.

 

I do not understand what I should be doing instead. I am a n00b working my way through a book that is not always as explicit as it needs to be. If I can't picture it in my head, or analyze it, I can't understand it. I do not know enough about this to be able to parse your comment, "When you changed the function definition so that it returns an array instead of a single value, $sum is now an array instead of a one value. To echo the elements of the array $sum, you must use array syntax, $sum[0] and $sum[1] instead of just $sum", into what exactly the code should look like. They way I learn, I need to see how the code should look like versus what I'm being told so I can make some sense of it. I still don't understand half of what I'm doing, so I don't really understand how the script is thinking. I did what the book suggested, and the script broke.

 

I'll just keep working my way through the book and perhaps one day I'll understand. Thank you, though.

At the point where you are printing the results now (after the calculate_total() function has been called and the result has been assigned to $sum), do the following -

 

echo $sum; // should print the word "Array"
echo "<br />"; // html new line to separate the lines being echoed
echo $sum[0]; // should be the total returned by the function
echo "<br />"; // html new line to separate the lines being echoed
echo $sum[1]; // should be the tax returned by the function
echo "<br />"; // html new line to separate the lines being echoed

 

You might consider shredding the book if you have already found inconsistencies in it and for the current problem if it directed you to change the function definition but not make the corresponding change to the code that is using the results of the function call (have you read one or two pages ahead to see if it does explain or use the results correctly?)

Yes, I've thought about shredding the book, but since the problems have not been frequent, just occasional, it seems a bit of an overreaction to destroy the book.

 

Since it's a tip, not it doesn't explain it more later. Thank you, though.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.