... in which we continue the teaching schtick ...
Conditional upon your acceptance...
Let's start with an answer to yesterays problem, shall we? There are better ways to do this, and there are more things you can do, but this is enough:
<HTML>
<FORM method=GET action="">
<INPUT NAME=a size=40 maxlength=8 VALUE=""> apples
at <INPUT NAME=prince_a size=40 maxlength=8 VALUE="">
each <BR>
<INPUT NAME=b size=40 maxlength=8 VALUE=""> breads
at <INPUT NAME=prince_b size=40 maxlength=8 VALUE="">
each <BR>
<INPUT NAME=c size=40 maxlength=8 VALUE=""> cakes
at <INPUT NAME=prince_c size=40 maxlength=8 VALUE="">
each <BR>
<input type=submit value="Calculate It">
</FORM>
<?php
$subtotala = $a * $price_a;
$subtotalb = $b * $price_b;
$subtotalc = $c * $price_c;
$total = $subtotala + $subtotalb + $subtotalc;
print ( "Total - $total" );
?>
</HTML>
Kicking it up a notch
Let's do something useful with these variables shall we? How about secret messages that only some people can see?
<HTML>
<FORM method=GET action="">
<INPUT TYPE=password NAME=login
size=40 maxlength=12 VALUE="">
<input type=submit value="Calculate It">
</FORM>
<?php
if ( $login == "secret" ) {
Print ( "This is a secret message. <BR>" );
}
?>
</HTML>
Try that out. If you put 'secret' in the password, you will get a message.
Unlike javascript, if you view don't put the password in, and view the source of the page - you still won't be able to see the message.
(Actually, MORAT has a much better password system - we'll get to that much later)
Explanations
Well, the new bit of code is fairly obvious there.
'if' means 'if whatever is inside the round brakcets () is true, then run the code inside the squiggly brackets {} Otherwise, don't.
== means 'is equal to' You can use all sorts of operators here - '<' for 'is less than' (which only really makes sense with numbers) and != for 'is not equal to'
You can do a bit more actually:
if ( $something == "option one" ) {
// Do something
} else if ( $something == "two" ) {
// Do second something
} else {
// Do something entirely different
}
Passing information back
One problem you have to remember is that your program will be run, from the start, every time the user goes to your page. It won't remember that they've been before, and neither will the users browser.
If you want to be different on their next visit, then you have to explicitly pass the information back and forth.
For example:
<HTML>
<FORM method=GET action="">
<?php
print ( "<INPUT NAME=a size=40 ");
print ( "maxlength=8 VALUE=\"$a\">" );
print ( " apples at <INPUT NAME=prince_a " );
print ( " size=40 maxlength=8 VALUE=\"$price_a\">" );
print ( " each <BR>\n" );
print ( "<INPUT NAME=b size=40 ");
print ( "maxlength=8 VALUE=\"$b\">" );
print ( " breads at <INPUT NAME=prince_b " );
print ( " size=40 maxlength=8 VALUE=\"$price_b\">" );
print ( " each <BR>\n" );
print ( "<INPUT NAME=c size=40 ");
print ( "maxlength=8 VALUE=\"$c\">" );
print ( " cakes at <INPUT NAME=prince_c " );
print ( " size=40 maxlength=8 VALUE=\"$price_c\">" );
print ( " each <BR>\n" );
$subtotala = $a * $price_a;
$subtotalb = $b * $price_b;
$subtotalc = $c * $price_c;
$total = $subtotala + $subtotalb + $subtotalc;
print ( "Total - $total" );
?>
<input type=submit value="Calculate It">
<BR>
</FORM>
</HTML>
Well - the first obvious difference is that we're printing the form out from within the php. Note the liberal use of '\n' and the fact that we use several print statements for each line.
Next spot the \" - these are used to tell php to literally print out a " symbol - whereas usually it would think it's the end of the line, and get all confused. (See how every string that we want to print has "s around it?)
When you try this out, you'll see that the numbers you enter stay in the form. Isn't that nice.
Actually, a little calculator like this is normally better done in javascript, because you don't have to send to the server every time you change a single value then.
Your turn
Combining the lessons you've learnt.
Make the calculator freak out if you spend more than your allotted budget. Say 50. Or maybe allow the user to enter their own budget?
If you are over budget, then say "No way" - obviously you'll need an if statement for this.
Final hint, remember that the statements happen in order, top to bottom. So you can't use something that you haven't set up yet.
Edited by Vitenka at 2001-06-06 10:10:26