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

Post by ok »

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


This is my (untested) AJAX code:

[syntax="html"]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Calculatron 5000 </TITLE>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
function AddNumber(number)
{
	document.all.eq.value = document.all.eq.value + number;
}

function Cal()
{
	var http_request = false;

	if(window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if(http_request.overrideMimeType)
		{
			http_request.overrideMimeType('text/html');
			// See note below about this line
		}
	}else if(window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if(!http_request)
	{
		alert('Giving up  Cannot create an XMLHTTP instance');
		return false;
	}	
    http_request.onreadystatechange = function() { alertContents(http_request); };
	string = document.all.eq.value;
	string = string.replace("+", "%2B");
    http_request.open('GET', "cal.php?eq=" + string, true);
    http_request.send(null);
	return true;
}

function alertContents(http_request, url)
{
	if(http_request.readyState == 4)
	{
		if(http_request.status == 200)
		{
			document.all.eq.value = http_request.responseText;
		}else{
			alert('There was a problem with the request.');
		}
	}
}
</SCRIPT>
</HEAD>

<BODY>
<FORM ACTION="cal.php" METHOD="GET" NAME="cal">
<TABLE border="0">
<TR>
	<TD colspan="4"><INPUT TYPE="text" NAME="eq"></TD>
</TR>
<TR>
	<TD><INPUT TYPE="button" value="1" name="b1" onClick="AddNumber('1');"></TD>
	<TD><INPUT TYPE="button" value="2" name="b2" onClick="AddNumber('2');"></TD>
	<TD><INPUT TYPE="button" value="3" name="b3" onClick="AddNumber('3');"></TD>
	<TD><INPUT TYPE="button" value="/" name="div" onClick="AddNumber('/');"></TD>
</TR>
<TR>
	<TD><INPUT TYPE="button" value="4" name="b4" onClick="AddNumber('4');"></TD>
	<TD><INPUT TYPE="button" value="5" name="b5" onClick="AddNumber('5');"></TD>
	<TD><INPUT TYPE="button" value="6" name="b6" onClick="AddNumber('6');"></TD>
	<TD><INPUT TYPE="button" value="*" name="multiply" onClick="AddNumber('*');"></TD>
</TR>
<TR>
	<TD><INPUT TYPE="button" value="7" name="b7" onClick="AddNumber('7');"></TD>
	<TD><INPUT TYPE="button" value="8" name="b8" onClick="AddNumber('8');"></TD>
	<TD><INPUT TYPE="button" value="9" name="b9" onClick="AddNumber('9');"></TD>
	<TD><INPUT TYPE="button" value="+" name="add" onClick="AddNumber('+');"></TD>
</TR>
<TR>
	<TD><INPUT TYPE="reset" value="C" name="reset"></TD>
	<TD><INPUT TYPE="button" value="0" name="b0" onClick="AddNumber('0');"></TD>
	<TD><INPUT TYPE="button" value="-" name="sub" onClick="AddNumber('-');"></TD>
	<TD><INPUT TYPE="button" value="=" name="equal" onClick="Cal();"></TD>
</TR>
</TABLE>	
</FORM>
</BODY>
</HTML>
100% HTML valid .[/syntax]

Code: Select all

<?php
$eq = $_GET['eq'];
$eq = str_replace("%2B", "+", $eq);
echo eval("return ($eq);");
?>
Enjoy!


Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Cool Ajax, but it is incredibly frightening to think that anyone would put this code on any server:

Code: Select all

<?php
$eq = $_GET['eq'];
$eq = str_replace("%2B", "+", $eq);
echo eval("return ($eq);");
?>
(#10850)
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

Yeah... I didn't spend time on the PHP part... It needs improvement!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

That Ajax code will end up putting "12+34" in the calculator display before you click "=" won't it? That's not like a real calculator. :)
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

No!!! It's like a real basic cal
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

ok wrote:No!!! It's like a real basic cal
It doesn't have to be basic to do that - an ordinary scientific calculator with a two line display does it as well.

aerodromoi
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

Here's my try ;)
It can handle only one operation at a time, though.

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>::calculator::</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
$current = $_GET['c'];
$itself  = $_SERVER['PHP_SELF']."?c=".$current;
$display = $current;

if(ereg("e", $current)){
  preg_match_all("/^([0-9]*)(m|p|d|x)([0-9]*)(e)/is", $current, $calcarray, PREG_SET_ORDER);
  
  switch($calcarray[0][2]){
    case m:
      $display = intval($calcarray[0][1]) - intval($calcarray[0][3]);
      break;
    case p:
      $display = intval($calcarray[0][1]) + intval($calcarray[0][3]);    
      break;    
    case x:
      $display = intval($calcarray[0][1]) * intval($calcarray[0][3]);    
      break;
    case d:
      if (intval($calcarray[0][3])==0) {$display = "error";}
      else{$display = intval($calcarray[0][1]) / intval($calcarray[0][3]);   }
      break;
  }
  $itself  = $_SERVER['PHP_SELF']."?c=";
}
?>
<table border="1" cellpadding="3" cellspacing="5">
<tr>
<td colspan="4" width="150" height="30"><?php echo $display; ?></td>
</tr>
<tr>
  <td><a href="<?php echo $itself; ?>1">1</a></td>
  <td><a href="<?php echo $itself; ?>2">2</a></td>
  <td><a href="<?php echo $itself; ?>3">3</a></td>
  <td><a href="<?php echo $itself; ?>p">+</a></td>
</tr>
<tr>
  <td><a href="<?php echo $itself; ?>4">4</a></td>
  <td><a href="<?php echo $itself; ?>5">5</a></td>
  <td><a href="<?php echo $itself; ?>6">6</a></td>
  <td><a href="<?php echo $itself; ?>m">-</a></td>
</tr>
<tr>
  <td><a href="<?php echo $itself; ?>7">7</a></td>
  <td><a href="<?php echo $itself; ?>8">8</a></td>
  <td><a href="<?php echo $itself; ?>9">9</a></td>
  <td><a href="<?php echo $itself; ?>x">x</a></td>
</tr>
<tr>
  <td><a href="<?php echo $itself; ?>0">0</a></td>
  <td><a href="<?php echo $itself; ?>d">/</a></td>
  <td><a href="<?php echo $_SERVER['PHP_SELF']; ?>">c</a></td>
  <td><a href="<?php echo $itself; ?>e">=</a></td>
</tr>
</table>
</body>
</html>
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Where did Raptor go?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Before we get too far ahead of ourselves, don't forget that this thread is for someone who has about 3 hours of PHP experience. If you're going to write something that introduces some of PHP's slightly non-newbie functionality (type casting for example) it'd be worthwhile commenting your code a little.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I thought we are opposed to writting code for people when it is for school :?

Do the right thing here, and contact your school administration because writting such an application is seriously out of your skill level (being 3 hours experience). I could barely figure out basic syntax within 3 hours..
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

I agree with Jcart here.

Schools, public and private, are giving impossible assignments more and more these days. Whether they have monetary, knowledge, or time prerequisites beyond that of the student, they are a growing problem and something has to be done about it by the students.

If these problems are allowed to continue without the intervention of parents or students, education in this country is going to fail. It failed me. I am not the first, and sure to not be the last. So I plead with the people who read this thread to do something about this problem.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Daedalus- wrote:Schools, public and private, are giving impossible assignments more and more these days.
This isn't an impossible assignment though. It's quite an interesting one actually, and should get you used to PHP's basics (forms, variables, arrays, operators, and a couple of basic functions). Of course, you could go a hell of a long way writing a super-duper calculator that did loads of stuff, but I don't think that's what the tutor is asking for here.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Let me reiterate something that another person posted.
Jcart wrote:I thought we are opposed to writting code for people when it is for school :?

Do the right thing here, and contact your school administration because writting such an application is seriously out of your skill level (being 3 hours experience). I could barely figure out basic syntax within 3 hours..
Jcart wrote:because writting such an application is seriously out of your skill level (being 3 hours experience).
Jcart wrote:3 hours experience.
I would figure that for the average person, with no scripting experience, and only a 3 hour crash course, this is an impossible assignment.

I've been through 3x as many hours of C# webcasts and still couldn't write a calculator.

Really though, the fact still remains that the problem exists and my statement still stands.

I would prefer to not let this take the thread off-topic though.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Daedalus- wrote:Schools, public and private, are giving impossible assignments more and more these days. Whether they have monetary, knowledge, or time prerequisites beyond that of the student, they are a growing problem and something has to be done about it by the students.
In a computer science course after having three classes in the basics of PHP, you should be able solve a simple problem like this. It is not a bad problem because it is really more a design problem than a coding one -- the ultimate code being fairly simple. Oh no! Students are being forced to think!
Daedalus- wrote:If these problems are allowed to continue without the intervention of parents or students, education in this country is going to fail. It failed me. I am not the first, and sure to not be the last. So I plead with the people who read this thread to do something about this problem.
Oh give me a break ... you sound like a politician rambling talking points to scare people into voting for him. Yeah right ... a growing problem of school being too hard!
(#10850)
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Too hard wasn't what I said.

My ex-girlfriend and I, while in high school, would regurlarly recieve assignments from the teacher that were well beyond her (or her parents) financial abilities. I have experienced this myself. Also, we have both recieved graded assignments that required not only transportation, but hours of time that neither of us had.

It does happen.
Post Reply