views:

184

answers:

4

What's the best way to create a PHP form that takes a users input in the form of five decimal numbers:

  • displays the five decimal numbers
  • display the sum of the five decimal numbers
  • display the average the five decimal numbers
  • display each number rounded to the nearest integer?
A: 

PHP has a huge array of functions, like round(), floor(), ceil() and echo. All you need.

I'd send the numbers via POST, check to see if any are coming through, if they are display the form, if not display the results. It's easy stuff, you'll get it in no time. (Though yes, get this to SO)

Phoshi
+4  A: 

Create an array for the values, and a specific cgi-key (just a text string). Use a string consisting of cgi-key and the array index to check if the values are returned by CGI, and assign them to the respective array indices.

If you didn't get the values, display a form for the user to provide the values (of course using the cgi-key with array index as keys). If you did get the values, perform the calculations and display the results.

As for the calculations, check out all the different PHP array functions, or just perform them manually in a loop.

Also, ask further questions related to programming at Stack Overflow. And if you need help with your homework please state so in the question, and when returning your work, state publicly in the exercise group (in presence of the teacher or supervisor) where you got the help. In this way other people learn of the resource too so they can get help learning as well. There's nothing wrong in asking for information to undestand a problem, but hiding the fact that it's your homework does seems quite dubious...

Ilari Kajaste
A: 

The first problem you need to solve is parsing the input. In this case that means parsing a string for 5 numeric values. Take a look at the php string functions and regular expressions (regex). Personally I'd solve this with regex.

Benedict Cohen
why would you not just use something like is_numeric (http://uk.php.net/manual/en/function.is-numeric.php) - much easier than a regex, I think :)
warren
A: 

Look at the $_POST[] built-in when getting values from the form you'll need for the five fields (one for each number).

All the functions you'll need are available in PHP natively: see the manual :) http://uk.php.net/manual/en/

warren