Performing number evaluations from input

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

johnny_control
Forum Newbie
Posts: 16
Joined: Sat Dec 13, 2008 5:42 am

Performing number evaluations from input

Post by johnny_control »

A feature I would like to add to my personal site is the ability to allow users to enter in some numbers and have the executed, for instance:

Code: Select all

<form action="sum.php" method="post">
 <p>
  <input type="text" name="num1" />
  <select name="method">
   <option value="+">+</option>
   <option value="-">-</option>
   <option value="*">x</option>
   <option value="/">/</option>
  </select>
  <input type="text" name="num2" />
 </p>
</form>
I know that I could probably use eval() for this, but that is probably too dangerous. In the future I would like to turn it into a pretty cool app, where they can add parenthesis, etc.

Would anyone happen to know how I could create this?
Thanks!
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Performing number evaluations from input

Post by jaoudestudios »

I would do it using php, no need to drop into the shell. Once you have it working in php, then you can progressively enhance it with javascript :)
johnny_control
Forum Newbie
Posts: 16
Joined: Sat Dec 13, 2008 5:42 am

Re: Performing number evaluations from input

Post by johnny_control »

jaoudestudios wrote:I would do it using php, no need to drop into the shell. Once you have it working in php, then you can progressively enhance it with javascript :)
Indeed. I was planning on doing it in PHP. But I don't know where to start.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Performing number evaluations from input

Post by jaoudestudios »

Its quite easy. What languages do you know, so I can try and find a similar one and relate the two together?

Something along these lines...

Code: Select all

 
<?php
if (!empty($_POST)) {
 // do your calculation
}
?>
<form action="sum.php" method="post">
 <p>
  <input type="text" name="num1" />
  <select name="method">
   <option value="+">+</option>
   <option value="-">-</option>
   <option value="*">x</option>
   <option value="/">/</option>
  </select>
  <input type="text" name="num2" />
 <input type="submit" name="submit" id="submit" value="Calculate" />
 </p>
</form>
 
johnny_control
Forum Newbie
Posts: 16
Joined: Sat Dec 13, 2008 5:42 am

Re: Performing number evaluations from input

Post by johnny_control »

I know PHP. Feel free to provide an example in PHP.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Performing number evaluations from input

Post by jaoudestudios »

ok cool.

In the example I gave just put the conditions (add/subtract etc...) for each in the conditional statement and do the calculation. I would use a switch not elseif.
johnny_control
Forum Newbie
Posts: 16
Joined: Sat Dec 13, 2008 5:42 am

Re: Performing number evaluations from input

Post by johnny_control »

jaoudestudios wrote:ok cool.

In the example I gave just put the conditions (add/subtract etc...) for each in the conditional statement and do the calculation. I would use a switch not elseif.
That would be silly. Once users start to use parenthesis, or I add modulus then it would become code spaghetti.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Performing number evaluations from input

Post by jaoudestudios »

johnny_control wrote:
jaoudestudios wrote:ok cool.

In the example I gave just put the conditions (add/subtract etc...) for each in the conditional statement and do the calculation. I would use a switch not elseif.
That would be silly. Once users start to use parenthesis, or I add modulus then it would become code spaghetti.
I thought you wanted it simple especially as you had nothing working yet. You can do it object orientated if you want. Create a calculation class with a method for each type of calculation.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Performing number evaluations from input

Post by jaoudestudios »

I would start with a base class that has simple calculations (+,-,/,x), then extend (or however you prefer to do it) with additional modules (sin,cos,tan etc) one at a time and test as you go along, otherwise it will become messy. Get each module working then tidy it up before going on to the next one. Also if you have problems with a module you can just post that module here and we can help.
johnny_control
Forum Newbie
Posts: 16
Joined: Sat Dec 13, 2008 5:42 am

Re: Performing number evaluations from input

Post by johnny_control »

jaoudestudios wrote:I thought you wanted it simple especially as you had nothing working yet. You can do it object orientated if you want. Create a calculation class with a method for each type of calculation.
Ok. How would I go about creating a class that could handle, for instance:

Code: Select all

((100 * (985 + (12 * 8) + 98)) / 12.5)
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Performing number evaluations from input

Post by jaoudestudios »

How would the user add that info to your online calculator with 2 input fields? (num1 & num2)
johnny_control
Forum Newbie
Posts: 16
Joined: Sat Dec 13, 2008 5:42 am

Re: Performing number evaluations from input

Post by johnny_control »

jaoudestudios wrote:How would the user add that info to your online calculator with 2 input fields? (num1 & num2)
It would actually be using javascript to dynamically create elements, such as a opening parenthesis, a number, an addition/subtraction/division/multiplication/etc system, another number and finally a closing parenthesis.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Performing number evaluations from input

Post by jaoudestudios »

johnny_control wrote:
jaoudestudios wrote:How would the user add that info to your online calculator with 2 input fields? (num1 & num2)
It would actually be using javascript to dynamically create elements, such as a opening parenthesis, a number, an addition/subtraction/division/multiplication/etc system, another number and finally a closing parenthesis.
I still recommend to do it with php and progressively enhance it with javascript later on. That way it downgrades gracefully.
johnny_control
Forum Newbie
Posts: 16
Joined: Sat Dec 13, 2008 5:42 am

Re: Performing number evaluations from input

Post by johnny_control »

jaoudestudios wrote:I still recommend to do it with php and progressively enhance it with javascript later on. That way it downgrades gracefully.
Right, okay. So do you know of any way to parse and execute an equation like I posted above?
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Performing number evaluations from input

Post by jaoudestudios »

((100 * (985 + (12 * 8) + 98)) / 12.5)
I would start by grabbing the inner 2 brackets with a regex and work outwards while keeping a running total. If this is looped it wont matter how many encapsulated brackets there are.

But like I said start simple.
Try something like this in this order...

Code: Select all

 
1. (2 + 3) * 2
2. (2 + 3) * (4 + 4)
etc...
 
Post Reply