HTML and Web Pages
Download
Report
Transcript HTML and Web Pages
PHP : Validating forms
and loops
Some things for helping you to
validate input
The isset function can be used to test if a php
variable is passed.
If the variable $_POST['firstName'] contains a value then
isset($_POST['firstName']) would evaluate to true.
The is_numeric function may be useful during input
validation to determine if the user entered a
legitimate number.
if you are expecting a number to be typed into an HTML form
field named age then is_numeric($_POST['age'] would
evaluate to true
Some things for helping you to
validate input
The empty function can be used to test if a php
variable contains a value or not.
If the variable $_POST['firstName'] contains a value then
empty($_POST['firstName']) would evaluate to false.
Other validation include
is_string($variableName)
is_float($variableName)
is_bool($variableName)
is_int($variableName)
Loops: while and do-while
$num = 1;
while ($num < 10) {
echo "This loop is executed $num times<br />";
$num++;
}
$num = 1;
do while {
echo "This loop is executed $num times <br />";
$num++;
} ($num < 10)
Loops: for Loops
for ($num=0; $num < 10; $num++) {
echo "This loop is executed $num times<br />"
}
You can terminate explicitly with a break statement.
$desired = 10;
for ($num=0; $num<100; $i++) {
echo "The loop is on iteration: $num <br />"
if ($num == $desired) {
echo "Found $desired";
break; // if you reach this part, then the loop is broken
}
}
pex4.html, pex4.php
Redo ie. loan calculator so that
Formula for amortization table
Once you calculate the monthly payment, M, which stays the same.
(a)
The interest payment, IP = O * I
where O is the outstanding loan & I is the monthly
interest rate
(b) Principle Paid For, P, is the amount of loan that went to
principle rather than interest: P = M – IP
(c) The new outstanding loan, O will now be
O=O–P
ie. Previous Outstanding balance – Principle Paid For
Repeat the above steps (a) to (c) – you need LOOP here to
determine all periods.
Formatting and toggling
PHP allows up to round off numbers e.g.
$num =2.467;
$num = round($num, 2); // $num should now displayed
as 2.47
Next: How to do alternate colors? There is a concept call
modulus or sometimes call quotient. i.e. ($num % 2)
means take $num and divided by 2 and tell me the
remainder. So the remainder would be 0 or 1. If it is 0 you
can do one thing like print the current row in blue and if it
is 1 you can print the row in gold. How to do that in php?
echo "<TR bgcolor=blue>";
or
echo "<TR bgcolor=yellow>";