php : validating forms and loops

8
PHP : Validating forms and loops

Upload: yael-perry

Post on 01-Jan-2016

14 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: PHP : Validating forms and loops

PHP : Validating forms and loops

Page 2: 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

Page 3: PHP : Validating forms and loops

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)

Page 4: PHP : Validating forms and loops

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)

Page 5: PHP : Validating forms and loops

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

}}

Page 6: PHP : Validating forms and loops

pex4.html, pex4.php

Redo ie. loan calculator so that

Page 7: PHP : Validating forms and loops

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 toprinciple rather than interest: P = M – IP

(c) The new outstanding loan, O will now be O = O – Pie. Previous Outstanding balance – Principle Paid For

Repeat the above steps (a) to (c) – you need LOOP here to determine all periods.

Page 8: PHP : Validating forms and loops

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>";