# calculator script

2 replies to this topic

### #1 olivia

olivia
• New Members
• Newbie
• 1 posts

Posted 18 April 2006 - 02:53 AM

Q1:Create a calculator script that allows the user to submit two numbers and choose an operation to perform on them (addition, multiplication, division, subtraction).

Q2:Use hidden fields with the script you created in exercise 1 to store and display the number of requests that the user has submitted.

the script which i found in other site:

<?php
/*Create a calculator script that allows the user to submit two numbers and choose
an operation to perform on them (addition, multiplication, division, subtraction).
Use hidden fields with the script you created in activity 1 to store and display the
number of requests that the user has submitted.*/

\$sum = array("add\"=>(\$fnum + \$snum), // stores calculations
\"subtract\"=>(\$fnum-\$snum),
\"multiply\"=>(\$fnum*\$snum),
\"divide\"=>(\$fnum/\$snum));

\$submissions = (isset(\$submissions))?++\$submissions:0; // keeps track of how many times user has clicked calculate

if ( (!isset(\$fnum)) || (!isset(\$snum)) ) // if nothing is set for both number values print welcome message.
{
\$message = \"Welcome to the calculation script!\";
}

if ( (!isset(\$fnum)) && (isset(\$snum)) )
{
\$message = \"Value is missing in the first number field.\";
}

if ( (isset(\$fnum)) && (!isset(\$snum)) )
{
\$message = \"Value is missing in the second number field.\";
}

if ( (isset(\$fnum)) && (isset(\$snum)) ) // if both first and second number have a value
{
{
}

if (\$operation[\"subtraction\"]==\"subtraction\")
{
\$message = \$num[\"subtract\"];
}

if (\$operation[\"multiplication\"]==\"multiplication\")
{
\$message = \$num[\"multiply\"];
}

if (\$operation[\"division\"]==\"division\")
{
\$message = \$num[\"divide\"];
}
}

?>
<html>
<title>Testing Page</title>
<body>

<p><b><?php print \$message?></b></p>
<p> </p>
<p>You have used the calculator <?php print \$submissions?> times.</p>

<form action=\"<?php print \$PHP_SELF?>\" method=\"POST\">
<p>First Number: <input type=\"text\" name=\"fnum\" value=\"<?php print \$fnum?>\"></p>
<p>Second Number: <input type=\"text\" name=\"snum\" value=\"<?php print \$snum?>\"></p>
<p> </p>
<select name=\"operation[]\">
<option>subtraction
<option>multiplication
<option>division
</select>
<p> </p>
<input type=\"hidden\" name=\"submissions\" value=\"<?php print \$submissions?>\">
<p><input type=\"submit\" value=\"calculate!\"></p>
</form>
</body>
</html>

But it doesn't work.

### #2 Ninjakreborn

Ninjakreborn
• Members
• Information Technology Specialist
• 3,922 posts
• Age:33

Posted 18 April 2006 - 03:38 AM

I sent this to another post earlier maybe it will help. at
under the mathimatical chapters, there is a lot there that might help you figure those answers out.

------

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.

ypirc
• Members