The teacher asks for the Impossible

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

User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

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

Post 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 >.<
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Raptor Cardel
Forum Newbie
Posts: 7
Joined: Mon Jun 05, 2006 10:58 am

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post 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
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

Do you have to write this in PHP or you can write it with (or only) in JS???
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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. ;)
(#10850)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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>
Last edited by onion2k on Mon Jun 05, 2006 4:32 pm, edited 1 time in total.
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

Not working...
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

ok wrote:Not working...
If you're running in safe mode then eval() probably won't work. Otherwise.. post an error message.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

How is it not working? Did you name it calc.php?
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

yeah!!!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
Post Reply