I am working on a small application which accepts a series of values, and then does a little calculation and displays a result. The form for entering the values looks like this:
<form action="$SERVER[PHP_SELF]" method ="post">
<table><tr><td align="right">
X-ring: </td><td> <input type="text" size="4" name="xring"> </td></tr><tr><td align="right">
10-ring: </td><td> <input type="text" size="4" name="10ring"> </td></tr><tr><td align="right">
9-ring: </td><td> <input type="text" size="4" name="9ring"> </td></tr><tr><td align="right">
8-ring: </td><td> <input type="text" size="4" name="8ring"> </td></tr><tr><td align="right">
7-ring: </td><td> <input type="text" size="4" name="7ring"> </td></tr><tr><td align="right">
6-ring: </td><td> <input type="text" size="4" name="6ring"> </td></tr><tr><td align="right">
5-ring: </td><td> <input type="text" size="4" name="5ring"> </td></tr><tr><td align="right">
4-ring: </td><td> <input type="text" size="4" name="4ring"> </td></tr><tr><td align="right">
3-ring: </td><td> <input type="text" size="4" name="3ring"> </td></tr><tr><td align="right">
2-ring: </td><td> <input type="text" size="4" name="2ring"> </td></tr><tr><td align="right">
1-ring: </td><td> <input type="text" size="4" name="1ring"> </td></tr>
</table>
<input type="hidden" name="stage" value="process">
<input type="submit" value="Submit">
</form>
Now the problem I have is each of the elements of the array $_POST will have to be treated slightly differently. The lame solution I am using now (which works just fine) looks like this:
function process_form() {
$score = 0;
$shots = 0;
echo 'Hello ' . $_POST['stage'] . '!<p>';
$shots = $shots + $_POST['xring'];
$current_score = $_POST['xring'] * 10;
echo 'X-ring = ' . $_POST['xring'] . ' × 10 = ' . $current_score . '<br>';
$score = $score + $current_score;
$shots = $shots + $_POST['10ring'];
$current_score = $_POST['10ring'] * 10;
echo '10-ring = ' . $_POST['10ring'] . ' × 10 = ' . $current_score . '<br>';
$score = $score + $current_score;
$shots = $shots + $_POST['9ring'];
$current_score = $_POST['9ring'] * 9;
echo '9-ring = ' . $_POST['9ring'] . ' × 9 = ' . $current_score . '<br>';
$score = $score + $current_score;
$shots = $shots + $_POST['8ring'];
$current_score = $_POST['8ring'] * 8;
echo '8-ring = ' . $_POST['8ring'] . ' × 8 = ' . $current_score . '<br>';
$score = $score + $current_score;
$shots = $shots + $_POST['7ring'];
$current_score = $_POST['7ring'] * 7;
echo '7-ring = ' . $_POST['7ring'] . ' × 7 = ' . $current_score . '<br>';
$score = $score + $current_score;
$shots = $shots + $_POST['6ring'];
$current_score = $_POST['6ring'] * 6;
echo '6-ring = ' . $_POST['6ring'] . ' × 6 = ' . $current_score . '<br>';
$score = $score + $current_score;
$shots = $shots + $_POST['5ring'];
$current_score = $_POST['5ring'] * 5;
echo '5-ring = ' . $_POST['5ring'] . ' × 5 = ' . $current_score . '<br>';
$score = $score + $current_score;
// et cetera... you get the idea
} // end of function process_form()
I suppose I could toss that echo/add stuff into a little function somewhere, but I still need to do a slightly different calculation on each element. I am reluctant to just base the multiplier on the element's position in the array, in case the form changes slightly. I don't think a switch statement really simplifies anything.
Is there a cleaner way to do this?