Page 2 of 4
Posted: Mon Jun 05, 2006 12:03 pm
by andym01480
A challenge!
When numbers are posted concatenate a string with the number pressed and then echo the string as the value="" in the number box.
When an operator is pressed concatenate a different string with number so far and the operator
When= is pressed execute the string.
Use <input type=hidden> to pass on the two strings each time the html is outputted.
Would that work guys???? Seen Everah's post b4 me - looks better!
Probaly need to use single quotes for the variables until = is pressed and then double quotes to parse the result
Posted: Mon Jun 05, 2006 12:03 pm
by Raptor Cardel
Or get me a smack across the head "I told you in PHP only, grasshopper!"
Silly guy...
So, If I was to do it completly in PHP Ev, What would I need to change in my coding there? I get a basic idea of what you say would work, yet.. I dont know enough of PHP to put it into code >.<
Posted: Mon Jun 05, 2006 12:09 pm
by RobertGonzalez
Raptor Cardel wrote:Or get me a smack across the head "I told you in PHP only, grasshopper!"
Silly guy...
So, If I was to do it completly in PHP Ev, What would I need to change in my coding there? I get a basic idea of what you say would work, yet.. I dont know enough of PHP to put it into code >.<
Basically for each page load, check to see what was posted and how (was it a number button clicked, an operator or equals). I would keep concatenating the numeric string until an operator was pressed, at which point I would read the concatenated string into a var of some type (hidden field, session var or cookie). The read the operator into a var, and do this all over again for the second and subsequent number strings. Once equals is pressed, take everything you have (the number strings and the operators) and actually execute the math on them and return the value, just like a calculator.
Posted: Mon Jun 05, 2006 12:12 pm
by Raptor Cardel
My appologies, I really hate having to be asked to be spoon fed.. But I have only the smallest idea of how to imput that Ev. Could you provide a sample of that so I have an idea? I learned HTml entirely by taking others code and looking at it, Modifying it and learning by application. For once I have no clue what to do
Posted: Mon Jun 05, 2006 12:53 pm
by RobertGonzalez
I'll consider putting something together for you. I am busier than snot right now so I can't promise you anything. Maybe someone else can help out this poster with some sample code.
Posted: Mon Jun 05, 2006 1:39 pm
by Christopher
Raptor Cardel wrote:Well, how about for curiosity sake we actually code it to submit with every button? We do have a dedicated server for this project...
Probably for a school project that is what the instructor intended. Just have every button submit its value to the PHP script and have the PHP be a simple state machine that checks what is submitted and does the calculation. There are a couple of ways to go with this.
- You will need to check each input to see if it is valid for the current state. Some examples: when you start you can get a + or - for the sign of the first value, but not a * or /, or once you have received a decimal point within a number you cannot receive another one, etc. etc. etc.
- You will need to send back error messages if a input error was made.
- You will need to maintain what has been typed so far in a hidden input (or the session if you instructor allows that). You might also need to maintain other state information in other hidden inputs as well.
- Then you need to do the calculations. You can either do them as you go every time an operator is clicked -- which will be fine for simple equations. Or you can wait until the equal sign is clicked and then parse the whole equation -- that would allow fancy stuff like parens.
All in all is sound like just the kind of academic problems you get in school.
Posted: Mon Jun 05, 2006 2:38 pm
by aerodromoi
arborint wrote:All in all is sound like just the kind of academic problems you get in school.
It doesn't make much sense using server resources for something that can be handled on the client side.
Personally, I'd realise the whole shebang either in actionscript or in javascript, preferably the former.
However, as your teacher seems to insist on php, I'd recommend stripping this
js calculator down. What you need is the javascript part that enables you to punch in the numbers. As soon as you press an operator, the page posts the number to itself, including it inside a hidden input field etc.
aerodromoi
Posted: Mon Jun 05, 2006 3:39 pm
by ok
Do you have to write this in PHP or you can write it with (or only) in JS???
Posted: Mon Jun 05, 2006 3:52 pm
by Christopher
aerodromoi wrote:It doesn't make much sense using server resources for something that can be handled on the client side.
Unless those "server resources" are for students to learn programming doing projects like this.

Posted: Mon Jun 05, 2006 4:18 pm
by onion2k
Ultra simple way of doing it .. literally a 2 minute hack .. and WELL DODGY from a security aspect (eval()ing code from a form = very, very bad idea). Should get you started at least.
Code: Select all
<?php
switch ($_POST['input']) {
case "C":
$sum = "";
$output = "";
break;
case "=":
$output = eval("return $sum;");
$sum = "";
break;
case "/":
case "*":
case "-":
case "+":
$sum .= $_POST['input'];
$output = "";
break;
default:
$sum .= $_POST['input'];
$output .= $_POST['input'];
break;
}
?>
<form action="calc.php" method="post">
<input type="hidden" name="sum" value="<?php echo $sum; ?>">
<table>
<tr><td colspan="5"><input type="text" name="output" value="<?php echo $output; ?>"></td></tr>
<tr>
<td><input type="submit" name="input" value="7"></td>
<td><input type="submit" name="input" value="8"></td>
<td><input type="submit" name="input" value="9"></td>
<td><input type="submit" name="input" value="/"></td>
<td></td>
</tr>
<tr>
<td><input type="submit" name="input" value="4"></td>
<td><input type="submit" name="input" value="5"></td>
<td><input type="submit" name="input" value="6"></td>
<td><input type="submit" name="input" value="*"></td>
<td></td>
</tr>
<tr>
<td><input type="submit" name="input" value="1"></td>
<td><input type="submit" name="input" value="2"></td>
<td><input type="submit" name="input" value="3"></td>
<td><input type="submit" name="input" value="-"></td>
<td></td>
</tr>
<tr>
<td><input type="submit" name="input" value="C"></td>
<td><input type="submit" name="input" value="0"></td>
<td><input type="submit" name="input" value="="></td>
<td><input type="submit" name="input" value="+"></td>
<td></td>
</tr>
</table>
</form>
Posted: Mon Jun 05, 2006 4:29 pm
by ok
Not working...
Posted: Mon Jun 05, 2006 4:31 pm
by onion2k
ok wrote:Not working...
If you're running in safe mode then eval() probably won't work. Otherwise.. post an error message.
Posted: Mon Jun 05, 2006 4:31 pm
by RobertGonzalez
How is it not working? Did you name it calc.php?
Posted: Mon Jun 05, 2006 4:34 pm
by ok
yeah!!!
Posted: Mon Jun 05, 2006 4:44 pm
by Christopher
OK Raptor Cardel you've been given a free gift ... now add the state stuff to deal with all the error conditions and post your code for the rest of the class here to see. Perhaps you should specify what all the allow states and illegal states are first.