A simple Calculator

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Software_Pyrate
Forum Newbie
Posts: 10
Joined: Sat May 22, 2010 3:00 am

A simple Calculator

Post 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;

?>
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: A simple Calculator

Post 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.
User avatar
Software_Pyrate
Forum Newbie
Posts: 10
Joined: Sat May 22, 2010 3:00 am

Re: A simple Calculator

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: A simple Calculator

Post 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.
User avatar
Software_Pyrate
Forum Newbie
Posts: 10
Joined: Sat May 22, 2010 3:00 am

Re: A simple Calculator

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: A simple Calculator

Post 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.
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: A simple Calculator

Post 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!
Post Reply