PHP 5/MySQL Programming- P23 ppsx

5 319 0
PHP 5/MySQL Programming- P23 ppsx

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

Thông tin tài liệu

Returning to the Petals Game At the beginning of this chapter I show you the Petals Around the Rose game. This game uses all the skills you have learned so far, including the new concepts from this chapter. If you haven’t already done so, play the game now so you can see how it works. Here’s the basic plan of the Petals game: Each time the page is drawn, it randomly generates five dice and calculates the correct number of petals based on a super- secret formula. The page includes a form that has a text area called guess for the user to enter the answer. The form also includes a hidden field called numPetals, which tells the program what the correct answer was. The Petals game doesn’t introduce anything new, but it’s a little longer than any of the other programs you’ve seen so far. I introduce the code in smaller chunks. All the code is shown in order, but not in one long code sample. Look on the CD for the program in its entirety. Starting HTML Like most PHP programs, the Petals game uses some HTML to set everything up. The HTML is pretty basic because PHP code creates most of the interesting HTML. <HTML> <head> <title>Petals Around the Rose</title> 88 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 CAN’T THE PROGRAM REMEMBER THE RIGHT ANSWER? Since the program generated the correct answer in the first place, you might be surprised to learn that the right answer must be hidden in the Web page and then retrieved by the same program that generated it. Each contact between the client and the server is completely new. When the user first plays the game, the page is sent to the browser and the con- nection is completely severed until the user hits the submit button. When the user submits the form, the Petals program starts over again. It’s possible the user plays the game right before he goes to bed, then leaves the page on the computer overnight. Meanwhile, a hundred other people might use the pro- gram. For now, use hidden data to help keep track of the user’s situation. Later in this book you learn some other clever methods for keeping track of the users’ situations. </head> <body bgcolor = “tan”> <center> <font face = “Comic Sans MS”> <h1>Petals Around the Rose</h1> I decided on a tan background with a whimsical font. This should give the program a light feel. Main Body Code The main PHP code segment has three main jobs: print a greeting, print the dice, and print the form for the next turn. These jobs are (appropriately enough) stored in three different functions. One goal of encapsulation is to make the main code body as clean as possible. This goal is achieved in the Petals game. <? printGreeting(); printDice(); printForm(); All the real work is passed off to the various functions, which are described shortly. Even before you see the functions themselves, you have a good idea what each function does and a good sense of the program’s overall flow. Encapsulat- ing your code and naming your functions well makes your code much easier to read and repair. The printGreeting() Function The printGreeting() function prints one of three possible greetings to the user. If the user has never called this program before, the program should provide a welcome. If the user has been here before, she has guessed the number of petals. That guess might be correct (in which case a congratulatory message is appro- priate) or incorrect, requiring information about what the correct answer was. The printGreeting() function uses a switch statement to handle the various options. function printGreeting(){ global $guess, $numPetals; if (empty($guess)){ print “<h3>Welcome to Petals Around the Rose</h3>”; 89 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 } else if ($guess = = $numPetals){ print “<h3>You Got It!</h3>”; } else { print <<<HERE <h3>from last try: </h3> you guessed: $guess<br><br> -and the correct answer was: $numPetals petals around the rose<br> HERE; } // end if } // end printGreeting This function refers to both the $guess and $numPetals variables, which are auto- matically created. You can use one global statement to make more than one vari- able global by separating the variables with commas. The $guess variable is empty if this is the first time the user has come to the pro- gram. If $guess is empty, I print a welcoming greeting. The user has guessed cor- rectly if $guess is equal to $numPetals, so I print an appropriate congratulations. If neither of these conditions is true (which is most of the time), the function prints out a slightly more complex string indicating the user’s last guess and the correct answer. This should give the user enough information to finally solve the riddle. The else if structure turns out to be the easiest option for handling the three possible conditions I want to check. The printDice() Function After the program prints a greeting, it does the important business of generating the random dice. It’s relatively easy to generate random dice, as you saw earlier in this chapter. However, I also wanted to be efficient and calculate the correct number of petals. To make the printDice() function more efficient, it calls some other custom functions. function printDice(){ global $numPetals; print “<h3>New Roll:</h3>”; $numPetals = 0; 90 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 91 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 $die1 = rand(1,6); $die2 = rand(1,6); $die3 = rand(1,6); $die4 = rand(1,6); $die5 = rand(1,6); showDie($die1); showDie($die2); showDie($die3); showDie($die4); showDie($die5); print “<br>”; calcNumPetals($die1); calcNumPetals($die2); calcNumPetals($die3); calcNumPetals($die4); calcNumPetals($die5); } // end printDice The printDice() function is very concerned with the $numPetals variable, but doesn’t need access to $guess. It requests access to $numPetals from the main pro- gram. After printing out the “New Roll” message, it resets $numPetals to 0. The value of $numPetals is recalculated each time the dice are rolled. I got new dice values by calling the rand(1, 6) function six times. I stored each result in a different variable, named $die1 to $die6. To print out an appropriate graphic for each die, I called the showDie() function. I printed out a line break, then called the calcNumPetals() function once for each die. The showDie() Function The showDie() function is used to simplify repetitive code. It accepts a die value as a parameter and generates the appropriate HTML code for drawing a die with the corresponding number of dots. function showDie($value){ print <<<HERE <img src = “die$value.jpg” height = 100 width = 100> HERE; } // end showDie One advantage of using functions for repetitive HTML code is the ease with which you can modify large sections of code. For example, if you wish to change image sizes, change the img tag in this one function. All six die images are changed. The calcNumPetals Function The printDice() function also calls calcNumPetals() once for each die. This func- tion receives a die value as a parameter. It also references the $numPetals global variable. The function uses a switch statement to determine how much to add to $numPetals based on the current die’s value. Here’s the trick: The center dot of the die is the rose. Any dots around the center dot are the petals. The value 1 has a rose but no petals; 2, 4, and 6 have petals, but no rose; 3 has two petals; 5 has four. If the die roll is 3, $numPetals should be increased by 2; if the roll is 5, $numPetals should be increased by 4. function calcNumPetals($value){ global $numPetals; switch ($value) { case 3: $numPetals + = 2; break; case 5: $numPetals + = 4; break; } // end switch } // end calcNumPetals The +=code is a shorthand notation. The line shown here $numPetals + = 2; is exactly equivalent to this line: $numPetals = $numPetals + 2; TRICK 92 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 . program in its entirety. Starting HTML Like most PHP programs, the Petals game uses some HTML to set everything up. The HTML is pretty basic because PHP code creates most of the interesting HTML. <HTML> <head> <title>Petals. background with a whimsical font. This should give the program a light feel. Main Body Code The main PHP code segment has three main jobs: print a greeting, print the dice, and print the form for the

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

Mục lụ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

Tài liệu cùng người dùng

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

Tài liệu liên quan