Page 1 of 1

A simple Calculator

Posted: Sun Jun 27, 2010 11:23 am
by Software_Pyrate
Hi guys, I'm a newbie and just gettin the hang of php. I am making a simple calculator which works when I manually give the operator like so...

$a + $b = $c

But I'm trying to make the operator itself a variable from a form and can't figure it out...I'm sure this is elementary for you guys...but could you help me out here. This is what I go t so far...

Code: Select all

<h1>The Calculator</h1>

<form method="post" action="dbselecct.php">
<input type="text" name="box1" />
<input type="text" name="kind" />
<input type="text" name="box2" />
<br/>
<input type="submit" value="Calculate" /><br/>
</form>


Total = 
<?php 
$box1 = $_POST['box1'];
$kind = $_POST['kind'];
$box2 = $_POST['box2'];

$total = ???????????????   //What goes here?
                                  //I just want to be able to put a + - / * % in the middle box,
                                  // hit submit and have it out put the answer

echo $total;

?>

Re: A simple Calculator

Posted: Sun Jun 27, 2010 11:43 am
by internet-solution
I will give you clues - operators in drop down box or radio button in form. Then use switch in PHP to calculate the correct answer.

Re: A simple Calculator

Posted: Mon Jun 28, 2010 6:15 pm
by Software_Pyrate
i appreciate the hint....but It doesn't help me as there are still 3 variables in the statement...i.e....

$a $b $c = $total


$a is the first test box...

$b is the operator...

$c is the second text box...

how do I write the $total because I can't do this...

$a $b $c = $total as $b is the operator i.e 1 + 2 = 3 where + is the varible.

I have successfully been able to do it with if statements....but that seems...not efficient
if($_POST['kind'] == +)
{
$a + $b = $total
}

or

if($_POST['kind'] == -)
{
$a - $b = $total
}

Come on guys.....tell me there's a better way.....I'm not asking you to code my site...just take some pitty on this poor sap

Re: A simple Calculator

Posted: Mon Jun 28, 2010 7:27 pm
by califdon
internet-solution gave you the answer, read his reply again. Use the 'switch' syntax to perform the arithmetic, based on what is in the middle (operator) box (or take his good suggestion that you use a drop down box instead of a text box for the operator, so the user is limited to valid operators which your script will recognize). Obviously this is a learning exercise for you, that's good, so this way you will learn about switch syntax and maybe drop down boxes, too.

Re: A simple Calculator

Posted: Tue Jul 06, 2010 4:51 pm
by Software_Pyrate
Hey guys...I think I got it....

I learned alot about floats and whole numbers man....Thanks for the advice. :D

Of course it could be improved....But it works! :)
Here's what Icame up with...

Code: Select all

<?php
if(isset($_POST['submit']))
{
	$box1 = $_POST['b1'];
	$operator = $_POST['op'];
	$box2 = $_POST['b2'];



switch($operator)
 {
	case "+":
	echo bcadd($box1,$box2,4);
	break;

	case "-":
	echo bcsub($box1 - $box2,3);
	break;

	case "*":
	echo bcmul($box1,$box2,3);
	break;

	case "/":
	echo bcdiv($box1,$box2,3);
	break;

	case "%":
	echo $box2 % $box1;
	break;
	
	default: echo " No values added";
 }


}

?>
once again...thanks for taking the time for this poor sap

Re: A simple Calculator

Posted: Tue Jul 06, 2010 6:45 pm
by califdon
You're welcome, that's why we spend time here, to help people who want to learn and are willing to invest their own energy into it. Hey, none of us were born knowing this stuff, we all learned it by studying and doing it. Congratulations on getting it working, good luck in your learning, and come back when you have more questions.

Re: A simple Calculator

Posted: Tue Jul 06, 2010 7:27 pm
by internet-solution
Software_Pyrate wrote:Hey guys...I think I got it....

I learned alot about floats and whole numbers man....Thanks for the advice. :D

Of course it could be improved....But it works! :)
Here's what Icame up with...



once again...thanks for taking the time for this poor sap
Glad to know that the advice you got here was useful and you got it worked. Congratulations!