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

PHP 5/MySQL Programming- P24 ppt

5 319 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

The first style is much shorter and easier to type, so it’s the form most programmers prefer. The printForm() Function The purpose of the printForm() function is to print the form at the bottom of the HTML page. This form is pretty straightforward except for the need to place the hidden field for $numPetals. function printForm(){ global $numPetals; print <<<HERE <h3>How many petals around the rose?</h3> <form method = “post”> <input type = “text” name = “guess” value = “0”> <input type = “hidden” name = “numPetals” value = “$numPetals”> <br> <input type = “submit”> </form> <br> <a href = “petalHelp.html” target = “helpPage”> give me a hint</a> HERE; } // end printForm This code places the form on the page. I could have done most of the form in plain HTML without needing PHP for anything but the hidden field. However, when I start using PHP, I like to have much of my code in PHP. It helps me see the flow of things more clearly (print greeting, print dice, and print form, for example). 93 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 94 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 The Ending HTML Code The final set of HTML code closes everything up. It completes the PHP segment, the font, the centered text, the body, and finally, the HTML itself. ?> </font> </center> </body> </html> Summary You learn a lot in this chapter. You learned several kinds of branching structures, including the if clause, else statements, and the switch structure. You know how to write functions, which make your programs much more efficient and easier to read. You know how to pass parameters to functions and return values from them. You can access global variables from inside functions. You put all these things together to make an interesting game. You should be very proud! In the next chapter you learn how to use looping structures to make your programs even more powerful. CHALLENGES 1. Write a program that generates 4-, 10-, or 20-sided dice. 2. Write a program that lets the user choose how many sides a die has and print a random roll with the appropriate maximum values. (Don’t worry about using images to display the dice.) 3. Write a Loaded Dice program that half the time generates the value 1 and half the time generates some other value. 4. Modify the story game from chapter 2, “Using Variables and Input,” so the form and the program are one file. 5. Create a Web page generator. Make a form for the page caption, background color, font color, and text body. Use this form to generate an HTML page. L o o p s a n d A r r a y s 4 CHAPTER Y ou know all a program’s basic parts, but your programs can be much easier to write and more efficient when you know some other things. In this chapter you learn about two very important tools, arrays and looping structures. Arrays are special variables that form lists. Looping structures repeat certain code segments. As you might expect, arrays and loops often work together. You learn how to use these new elements to make more interesting programs. Specifically, you do these things: • Use the for loop to build basic counting structures • Modify the for loop for different kinds of counting • Use a while loop for more flexible looping • Identify the keys to successful loops • Create basic arrays • Write programs that use arrays and loops • Store information in hidden fields Introducing the Poker Dice Program The main program for this chapter is a simplified dice game. In this game, you are given $100 of virtual money. On each turn, you bet two dollars. The computer rolls five dice. You can elect to keep each die or roll it again. On the second roll, the computer checks for various combinations. You can earn money back for rolling pairs, triples, four or five of a kind, and straights (five numbers in a row). Figures 4.1 and 4.2 illustrate the game in action. The basic concepts of this game are much like the ones you use in other chapters’ programs. Keeping track of all five dice can get complicated, so this program uses arrays and loops to manage all the information. Counting with the for Loop You might want the computer to repeat some sort of action multiple times. Good thing computers excel at repetitive behavior. For example, take a look at the simpleFor.php program shown in Figure 4.3. 96 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 FIGURE 4.1 After the first roll, you can keep some of the dice by selecting the checkboxes underneath each die. While the output of the simpleFor.php program doesn’t look all that interesting, it has a unique characteristic. It has only one print statement in the entire pro- gram, which is executed 10 different times. Take a look at the source code to see how it works: <html> 97 C h a p t e r 4 L o o p s a n d A r r a y s FIGURE 4.2 The player has earned back some money with a full house! FIGURE 4.3 This program counts from zero to one using only one print statement. . of the form in plain HTML without needing PHP for anything but the hidden field. However, when I start using PHP, I like to have much of my code in PHP. It helps me see the flow of things more. 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 94 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 The Ending HTML Code The final set of HTML code closes everything up. It completes the PHP segment, the font, the centered text, the body, and finally, the HTML itself. ?> </font> </center> </body> </html> Summary You. Good thing computers excel at repetitive behavior. For example, take a look at the simpleFor .php program shown in Figure 4.3. 96 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 FIGURE

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

TỪ KHÓA LIÊN QUAN