Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Lash DePaul University SNL 262 Web Page Design Web Wizard’s Chapter 3 Instructor: David A. Lash Controlling the instruction flow.

Similar presentations


Presentation on theme: "David Lash DePaul University SNL 262 Web Page Design Web Wizard’s Chapter 3 Instructor: David A. Lash Controlling the instruction flow."— Presentation transcript:

1 David Lash DePaul University SNL 262 Web Page Design Web Wizard’s Chapter 3 Instructor: David A. Lash Controlling the instruction flow

2 David Lash 2 What We Learned Last Time zPHP is a technology that can help create dynamic pages. yPHP instructions run before HTML page is returned to browser yPHP output is mixed in with HTML statements zPHP instructions are placed within these tags x zPHP requires a more precise syntax that HTML zLooks like yprint ( “ Hello There ”);  PHP instructions can only be interpreted on Web server

3 David Lash 3 More of What We Learned Last Time zWe can temporarily save data in PHP variables zWe can store numbers or character strings y$x = $y + 2; y$apple = $x + 5; y$first = “George”; y$last = “Bush”; y$full = “$first W. $last”; zWe can pass data from HTML forms to PHP scripts  The tag specifies next file to start. yWe specify data to pass as follows within the HTML form:  yReceive as follows within PHP: x$name=$_POST[‘firstname’];

4 David Lash Objectives zSo far we’ve received data but cannot do much behind mail that data to us. zWill look at: yconditional test statements – Can use to look at: xWas needed data supplied? xTest data vales – Is data > 50? Is aver > 90? zLook at testing more than one thing at once: yCreate compound conditional test statements

5 David Lash 5 Using Conditional Test Statements zConditional statements provide a way for scripts to test for certain data values and then to react differently depending on the value found.  Will examine ythe if statement, ythe elseif clause,  the else clause.

6 David Lash 6 Using the if Statement  Use an if statement to specify a test condition and a set of statements to run when a test condition is true. if ($average > 69) { $Grade="Pass"; print "Grade=$Grade "; } print "Your average was $average";  if $average was equal to 70 then the above would output: Your average was 70 When $average is greater than 69 execute these statements.

7 David Lash 7 Note the strange syntax if ($average > 69) { $Grade="Pass"; print "Grade=$Grade "; } print "Your average was $average"; Expression to test When true do everything between the { } Note how the if statement doesn’t use semicolons. But everything else does. Instead use the { … } block.

8 David Lash 8 Test Expressions zTest expressions use test operators within their expressions. yTest operators work much like the expression operators. yThe if statement above uses the greater than (>) operator to test whether $average is greater than 69. yTest operators evaluate to true or false

9 David Lash 9 PHP Test Operators

10 David Lash 10 A Full Example... zConsider the following application: yReceives two grades as input and determines whether their average is above 89. y It uses an HTML form for input grades: yEnter First Score yEnter Second Score Sets $grade1 Sets $grade2

11 David Lash 11 Receiving Code 1. 2. Decisions 3. 4.<?php 5. $grade1= $POST[“grade1”]; 6. $grade2= $POST[“grade2”]; 5. $average = ($grade1 + $grade2) / 2; 6. if ( $average > 89 ) { 7. print "Average score: $average You got an A! "; 8. } 9. $max=$grade1; 10. if ($grade1 < $grade2) { 11. $max = $grade2; 12. } 13. print ("Your max score was $max"); 14. ?> 15. Get grade1 and grade2 from HTML form. Calculate average Output if $average is more than 89 Set when $grade2 is more than $grade1

12 David Lash 12 Another more useful example 1. 2. 3. Survey Form 4. 5. 6. Class Survey 7. 8.What is the smallest planet in our solar system? 9. Mercury 10. Venus 11. Pluto 12. Our Moon 13. 14. 15. 16. 17. The front-end HTML form

13 David Lash 13 The receiving php 1. 2. 3. Survey Form 4. 5. 6. 7. Your Averages Are: 8. 9.<?php 10. $pick = $_POST[pick]; 11. $total_right = 0; 12. if ( $pick == 'c' ) { 13. print "You got question 1 right"; 14. $total_right = $total_right + 1; 15. } else { 16. print "You got question 1 wrong"; 17. } 18. print " pick=$pick total_right=$total_right"; 19.?> 20. 21. The receiving file with PHP http://condor.depaul.edu/~dlash/extra/Webpage/examples/planet.html

14 David Lash 14 Comparing Strings zPHP represents strings using the ASCII (“ask- ee”) code values. (American Standard Code for Information Interchange). yASCII provides a standard, numerical way to represent characters on a computer. yEvery letter, number, and symbol is translated into a code number. x“A” is ASCII code 65, “B” is 66, “C” is 67, and so on. xLowercase “a” is ASCII code 97, “b” is 98, “c” is 99, and s xASCII “A” is less than ASCII “a,” “B” is less than “b,” and “c” is less than “d”. xASCII characters have ASCII code values lower than letters. So ASCII character “1” is less than “a” or “A”

15 David Lash 15 Comparing Strings  You can use == operator to check if one string is equal to another. For example, $name1 = "George"; $name2 = "Martha"; if ($name1 == $name2) { print ("$name1 is equal to $name2" ); } else { print ("$name1 is not equal to $name2"); }  Would output : “ George is not equal to Martha ”.

16 David Lash 16 Comparing Strings  Also can use, = operators to compare string values using ASCII code values.  For Example $name1 = "George"; $name2 = "Martha"; if ($name1 < $name2) { print ("$name1 is less than $name2"); } else { print ("$name1 is not less than $name2"); }  It would output “ George is less than Martha ”.

17 David Lash 17 A Full Example... zConsider the following application: yCompares two input strings.  It uses the HTML form element that sets the variables $first and $second. First Name: <input type="text" size="10" maxlength="15" name="first"> Second Name: <input type="text" size="10" maxlength="15" name="second"> Sets $first Sets $second

18 David Lash 18 Receiving Code 1. 2. String Comparison Results 3. 4. <?php 5. $first = $_POST[“first”]; 6. $second = $_POST[“second”]; 7. print ("First=$first Second=$second "); 8. if ($first == $second) { 9. print ("$first and $second are equal"); 10. } 11. if ($first < $second) { 12. print ("$first is less than $second"); 13. } 14. if ($first > $second) { 15. print ("$first is greater than $second"); 16. } 17. ?> Output if $first is equal to $second Set when $second is less than $first Set when $first is more than $second Get the values of $first and $second

19 David Lash 19 The Output... The previous code can be executed at http://webwizard.awl.com/~phppgm/C3/comparenames.html

20 David Lash 20 Using the elseif Clause  Use an elseif clause with an if statement to specify an additional test condition if (test expression) { one or more PHP statements } elseif (test expression) one or more PHP statements } zThe above script checks the elseif test expression when the test condition for the if statement is false.

21 David Lash 21 Using the elseif Clause  One or more elseif clauses can be used with an if statement. if ($hour < 9) { print "Sorry, it is too early."; } elseif ($hour < 12) { print "Good morning. The hour is $hour. "; print "How can we help you?"; } elseif ($hour < 13) { print "Sorry, we are out to lunch. "; } elseif ($hour < 17) { print "Good afternoon. The hour is $hour. "; print "How can we help you?"; } elseif ($hour <= 23) { print "Sorry, we have gone home already."; } Check this test expression when the first three conditions are all false. Check this test expression when the first two conditions are all false. Check this test expression when the first condition is false. if $hour == 15, output “Good afternoon. The hour is 15. How can we help you?” if $hour == 24, then this code outputs nothing.

22 David Lash 22 Using the else Clause zUse an else clause with if and possibly one or more elseif clauses ySpecify set of statements to run when all the previous test conditions are false.  Has the following general format shown in the if (test expression) { one or more PHP statements } else { one or more PHP statements }

23 David Lash 23 Using the else Clause  For example, if $count had a value of –75, then this code would output “Illegal value for count = –75” if ( $count == 0 ) { print ("Time to reorder."); $reorder=1; } elseif ( $count == 1 ) { $reorder=1; print ("Warning: we need to start reordering."); } elseif ( $count > 1 ) { $reorder = 0; print ("We are OK for now."); } else { print ("Illegal value for count = $count"); }

24 David Lash 24 A Full Example... zFull example that extends the grade-averaging to determine a letter grade (A, B, C, D, or F) and to catch illegal input.  Use the following HTML form for input Sets $grade1 Sets $grade2 Enter First Score <input type="text" size="4” maxlength="7" name="grade1"> Enter Second Score <input type="text” size="4” maxlength="7" name="grade2">

25 David Lash 25 Receiving Code 1. Grade Calculation 2. 3. <?php 4. $grade1 = $_POST[“grade1”]; $grade2 = $_POST[“grade2”]; 5. $average = ($grade1 + $grade2) / 2; 6. if ($average > 89) { 7. print ("Average=$average You got an A"); 8. } elseif ($average > 79) { 9. print ("Average=$average You got a B"); 10. } elseif ($average > 69) { 11. print ("Average=$average You got a C"); 12. } elseif ($average > 59) { 13. print ("Average=$average You got a D"); 14. } elseif ($average >= 0) { 15. print ("Grade=$grade You got an F"); 16. } else { 17. print ("Illegal average less than 0 average=$average"); 18. } 19. $max=$grade1; 20. if ($grade1 < $grade2) { 21. $max = $grade2; 22. } 23. print (" Your max score was $max"); 24. ?> Compute average of $grade1 and $grade2 Check if $average is an “A”, “B”,”C”, “D” or “F” Get values of $grade1 and $grade2

26 David Lash 26 Would output the following... The previous code can be executed at http://perl-pgm.com/C3/decision.php

27 David Lash 27 Logical Test Operators zPHP supports three logical test operators. 1. &&—the AND operator. Use in if statements and while loops. Example : if ($ctr < $max && $flag == 0) { Do instructions in brackets when $ctr < $max AND $flag is 0.

28 David Lash 28 Or operator  2. ||— the OR operator. Used much like the AND operator in if statements andwhile loops. Example,  if ($ctr != $max || $flag == 0) { Carries out the statements within the if statement if either $ctr is not equal to $max or $flag is equal to 0.

29 David Lash 29 Not operator  3. !— the NOT operator. Used to test whether an expression is false (used in while loops and in if statements). Example, yif (!$flag == 0) { This statement is true when $flag is anything except 0.

30 David Lash 30 Example Front End Form zExample asks the user to guess a “secret” two-digit combination, uses logical test operators.  The Input HTML form uses the following to set pick1. A similar group sets a variable pick2. Pick a number from 1 to 9 1 2 3 4 5 6 7 8 9

31 David Lash 31 A Full Script Example … 1. Number Guess Results 2. 3. <?php 4. $pick1 =$_POST[“pick1”]; $pick2 =$_POST[“pick2”]; 5. $combo1=5; 6. $combo2=6; 7. if (($pick1 == $combo1) && ($pick2 == $combo2)) { 8. print ("Congratulations you got both secret numbers $combo1 $combo2!"); 9. } elseif (($pick1 == $combo1) || ($pick2 == $combo2)){ 10. print ("You got one number right."); 11. } else { 12. print ("Sorry, you are totally wrong!"); 13. } 14. print ("You guessed $pick1 and $pick2."); 15. ?>

32 David Lash 32 The Output... The previous code can be executed at http://perl-pgm.com/C3/drivelogical.html

33 David Lash 33 Sending Form A Simple Form Please enter your name: Can we contact you? Yes No http://condor.depaul.edu/~dlash/extra/Webpage/examples/contact.html

34 David Lash 34 Receiving Script <?php $contact= $_POST["contact"]; $fname= $_POST["fname"]; if (!$_POST[‘contact’]) { print "Please supply a contact information "; print “contact=$contact postcontact= $_POST[‘contact’]”; } elseif ( !$_POST[‘fname’] ) { print "Please supply a contact name "; print “name=$fname postname=$_POST[‘fname’] ”; } print " Contact=$contact name=$fname"; ?> Question: Why cann’t you do this test? if (!$contact) { instead of if( $_POST[‘contact’]) {

35 David Lash 35 Another Example … Consider average example Survey Form Class Survey Pick A Number: Pick A Number 2: Pick A Number 3:

36 David Lash 36 PHP Code 1. Guess the Dice 2. 3. Your Averages Are: 4. 5.<?php 6. $num1 = $_POST[num1]; 7. $num2 = $_POST[num2]; 8. $num3 = $_POST[num3]; 9. if ( !$_POST[‘num1’] || !$_POST[‘num2’] || !$_POST[‘num3’]) { 10. print "error please fill in values for all three numbers"; 11. print "num1=$num1 num2=$num2 num3=$num3"; 12. exit; 13. } 14. $aver = ($num1 + $num2 + $num3 ) / 3; 15. print " "; 16. print "num1 = $num1 "; 17. print "num2 = $num2 "; 18. print "num3 = $num3 "; 19. print " "; 20. print " aver = $aver"; 21. print " "; 22.?> 23. 24. http://condor.depaul.edu/~dlash/extra/Webpage/examples/newaverage.html

37 David Lash 37 One More Thing zYou can implement very basic password access using the following form element: y zYou can then use conditional statements to verify password. zThis is loose security. yIt sends password in unencryted between client and server. yIt will stop simple access to page (but not others in directory.) yMost use more sophisticated methods.

38 David Lash 38 Consider the following input form... A Simple Form Please enter your name: Please enter password: You can view this at http://condor.depaul.edu/~dlash/website/formpass.html http://condor.depaul.edu/~dlash/website/formpass.html

39 David Lash 39 Here is the receiving code... Receiving Script <?php $passwd= $_POST["pass"]; $fname= $_POST["fname"]; if ($passwd == "password" ) { print "Thank you $fname welcome "; print "Here is my site's content"; } else { print "Hit the road jack you entered password=$passwd "; print "Contact someone to get the passwd"; } ?>

40 David Lash 40 Summary zLooked at using conditional statements yif statement yelsif statement yelse statement zconditional statements have different format if ( $x < 100 ) { $x = $y + 1; $z = $y + 2; } zCan do multiple tests at once: if ( $x < 100 && $name = “george” ) { $x = $y + 1; $z = $y + 2; }  Can test if variable(s) set from form zif ( !$_POST[‘var1’] || !$_POST[‘var1’] ) {

41 David Lash 41 Validating and checking PHP? zhttp://hapedit.free.frhttp://hapedit.free.fr zA Free editor for html, php and other things. 1.Need to download the tool 2.Need to download php and install on windows machine http://www.php.net


Download ppt "David Lash DePaul University SNL 262 Web Page Design Web Wizard’s Chapter 3 Instructor: David A. Lash Controlling the instruction flow."

Similar presentations


Ads by Google