1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP 5/MySQL Programming- P20 pptx

5 252 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Cấu trúc

  • PHP 5 / MySQL Programming for the Absolute Beginner

    • Cover

  • Contents

  • Introduction

  • Chapter 1: Exploring the PHP Environment

  • Chapter 2: Using Variables and Input

  • Chapter 3: Controlling Your Code with Conditions and Functions

  • Chapter 4: Loops and Arrays

  • Chapter 5: Better Arrays and String Handling

  • Chapter 6: Working with Files

  • Chapter 7: Writing Programs with Objects

  • Chapter 8: XML and Content Management Systems

  • Chapter 9: Using MySQL to Create Databases

  • Chapter 10: Connecting to Databases within PHP

  • Chapter 11: Data Normalization

  • Chapter 12: Building a Three-Tiered Data Application

  • Index

  • Team DDU

Nội dung

<body> <h1>Hi User</h1> <? if (empty($userName)){ print <<<HERE <form> Please enter your name: <input type = “text” name = “userName”><br> <input type = “submit”> </form> HERE; } else { print “<h3>Hi there, $userName!</h3>”; } //end ?> </body> </html> This program begins by looking for the existence of a variable called $userName. There is no $userName variable the first time the program is called, because the program was not called from a form. The empty() function returns the value true if the specified variable is empty; it returns false if it has a value. If $userName does not exist, empty($userName) evaluates as true. The condition (empty($userName)) is generally true if this is the first time this page has been called. If it’s true, the program should generate a form so the user can enter her name. If the condition is false, that means somehow the user has entered a name (presumably through the form) so the program greets the user with that name. The key idea here is that the program runs more than once. When the user first links to hiUser.php, the program creates a form. The user enters a value on the form, and presses the submit button. This causes exactly the same program to be run again on the server. This time, though, the $userName variable is not empty, so rather than generating a form, the program uses the variable’s value in a greeting. Server-side programming frequently works in this way. It isn’t uncommon for a user to call the same program many times in succession as part of solving a par- ticular problem. You often use branching structures such as the if and switch statements to direct the program flow based on the user’s current state of activity. 73 C h a p t e r 3 C o n t r o l l i n g Y o u r C o d e w i t h C o n d i t i o n s a n d F u n c t i o n s Responding to Checkboxes Now that you know how to use the if structure and the empty function, you can work with checkboxes. Take a look at the following HTML code: <html> <head> <title>Checkbox Demo</title> </head> <body> <h1>Checkbox Demo</h1> <h3>Demonstrates checkboxes</h3> <form action =”checkDemo.php”> <h3>What would you like with your order?</h3> <ul> <li><input type =”checkbox” name =”chkFries” value =”1.00”>Fries </li> <li><input type =”checkbox” name =”chkSoda” value =”.85”>Soda </li> <li><input type =”checkbox” name =”chkShake” value =”1.30”>Shake </li> <li><input type =”checkbox” name =”chkKetchup” value =”.05”>Ketchup </li> </ul> <input type =”submit”> </form> </body> </html> 74 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r 75 C h a p t e r 3 C o n t r o l l i n g Y o u r C o d e w i t h C o n d i t i o n s a n d F u n c t i o n s This code generates the printout shown in Figure 3.12. When the user submits this form, it calls checkDemo.php, which looks like Figure 3.13. FIGURE 3.12 This page has a series of checkboxes. FIGURE 3.13 Notice that checkbox variables have a value or don’t exist. Checkboxes are a little different from other form elements, which consistently return a name/value pair. Checkboxes also have a name and a value, but the checkbox variable is sent to the server only if the box has been checked. As an example, compare Figures 3.12 and 3.13. You can see that only two of the check- boxes were selected. These checkboxes report values. If a checkbox isn’t selected, its name and value are not reported to the program. Take a look at the code for the checkDemo.php program to see how this works: <html> <head> <title>Checkbox Demo</title> </head> <body> <h3>Demonstrates reading checkboxes</h3> <? print <<<HERE chkFries: $chkFries <br> chkSoda: $chkSoda <br> chkShake: $chkShake <br> chkKetchup: $chkKetchup <br> <hr> HERE; $total = 0; if (!empty($chkFries)){ print (“You chose Fries <br> \n”); $total = $total + $chkFries; } // end if if (!empty($chkSoda)){ print (“You chose Soda <br> \n”); $total = $total + $chkSoda; } // end if if (!empty($chkShake)){ 76 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r 77 C h a p t e r 3 C o n t r o l l i n g Y o u r C o d e w i t h C o n d i t i o n s a n d F u n c t i o n s print (“You chose Shake <br> \n”); $total = $total + $chkShake; } // end if if (!empty($chkKetchup)){ print (“You chose Ketchup <br> \n”); $total = $total + $chkKetchup; } // end if print “The total cost is \$$total \n”; ?> </body> </html> The first part of the program simply prints out the expected variables. As you can see, if the checkbox has not been selected, the associated variable is never created. You can use the empty() function to determine if a checkbox has been checked. If the variable is empty, the corresponding checkbox was not checked. I used the negation operator ( !) to check for the existence of a variable. The condition (!empty($chkFries)) is true if chkFries was selected, and false otherwise. I tallied the values associated with all the selected checkboxes to get a grand total. Using Functions to Encapsulate Parts of the Program It hasn’t taken long for your programs to get complex. As soon as the code gets a little bit larger than a screen in your editor, it gets much harder to track. Pro- grammers like to break up code into smaller segments called functions to help keep everything straight. A function is like a miniature program. It is designed to do one job well. Look at Figure 3.14 for an example. Examining the This Old Man Program Song lyrics often have a very repetitive nature. The “This Old Man” song shown in Figure 3.14 is a good example. Each verse is different, but the chorus is always the same. You write each verse when you write the lyrics to such a song, but only write the chorus once. After that, you simply write “chorus.” This works very much like functions in programming language. The code for the This Old Man program illustrates: . name. The key idea here is that the program runs more than once. When the user first links to hiUser .php, the program creates a form. The user enters a value on the form, and presses the submit button Demo</title> </head> <body> <h1>Checkbox Demo</h1> <h3>Demonstrates checkboxes</h3> <form action =”checkDemo .php > <h3>What would you like with your order?</h3> <ul> <li><input. generates the printout shown in Figure 3.12. When the user submits this form, it calls checkDemo .php, which looks like Figure 3.13. FIGURE 3.12 This page has a series of checkboxes. FIGURE 3.13 Notice

Ngày đăng: 07/07/2014, 02:20

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN