The teacher asks for the Impossible
Moderator: General Moderators
- andym01480
- Forum Contributor
- Posts: 390
- Joined: Wed Apr 19, 2006 5:01 pm
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
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
Last edited by andym01480 on Mon Jun 05, 2006 2:42 pm, edited 2 times in total.
-
Raptor Cardel
- Forum Newbie
- Posts: 7
- Joined: Mon Jun 05, 2006 10:58 am
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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.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 >.<
-
Raptor Cardel
- Forum Newbie
- Posts: 7
- Joined: Mon Jun 05, 2006 10:58 am
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
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
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.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...
- 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.
(#10850)
- aerodromoi
- Forum Contributor
- Posts: 230
- Joined: Sun May 07, 2006 5:21 am
It doesn't make much sense using server resources for something that can be handled on the client side.arborint wrote:All in all is sound like just the kind of academic problems you get in school.
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
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
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>
Last edited by onion2k on Mon Jun 05, 2006 4:32 pm, edited 1 time in total.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US