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

PHP 5/MySQL Programming- P16 potx

5 266 0

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 172,68 KB

Nội dung

I made this program as simple as possible, because I didn’t expect to need it for long. It’s simply a table with the name of each variable and its associated value. I did it this way to ensure that I get all the variables exactly the way I want them. There’s no point in building the story if you don’t have the variables working. Building the Final Story The story itself is very simple to build if you’ve planned and ensured that the vari- ables are working right. All I had to do was write out the story as it was written in the plan, with the variables incorporated in the appropriate places. Here’s the code for the finished story.php page: <html> <head> <title>Little Boy Who?</title> </head> <body> <center> <h1>Little Boy Who?</h1> <? print <<<HERE <h3> Little Boy $color, come blow your $instrument!<br> The $anim1’s in the $place, the $anim2’s in the $vegetable.<br> Where’s the boy that looks after the $anim3?<br> He’s under the $structure, $action. </h3> HERE; ?> </center> </body> </html> It might astonish you that the final program is quite a bit simpler than the test program. Neither is very complicated, but once you have created the story, set up 53 C h a p t e r 2 U s i n g V a r i a b l e s a n d I n p u t the variables, and ensured that all the variables are sent correctly, the story pro- gram itself turns out to be almost trivial. Most of the story.php code is plain HTML. The only part that’s in PHP is one long print statement, which uses the print <<<HERE syntax to print a long line of HTML text with PHP variables embed- ded inside. The story itself is this long concatenated text. Summary In this chapter you learn some incredibly important concepts: what variables are, and how to create them in PHP; how to connect a form to a PHP program with modifications to the form’s method and action attributes; and how to write nor- mal links to send values to server-side scripts. You built programs that respond to various kinds of input elements, including drop-down lists, radio buttons, and list boxes. You went through the process of writing a program from beginning to end, including the critical planning stage, creating a form for user input, and using that input to generate interesting output. 54 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 CHALLENGES 1. Write a Web page that asks the user for his first and last name and then uses a PHP script to write a form letter to that person. Inform the user he might be a millionaire. 2. Write a custom Web page that uses embedded data tricks to generate custom links for your favorite Web searches, local news and weather, and other elements of interest to you. 3. Write your own story game. Find or write some text to modify, create an appropriate input form, and output the story with a PHP script. 3 M ost of the really interesting things you can do with a computer involve letting it make decisions. Actually, the computer only appears able to decide things. The programmer generates code that tells the computer exactly what to do in different circumstances. In this chapter, you learn how to control the flow of a program; specifically, how to: • Create a random integer • Use the if structure to change the program’s behavior • Write conditions to evaluate variables • Work with the else clause to provide instructions when a condition is not met • Use the switch statement to work with multiple choices • Build functions to better manage code • Write programs that can create their own forms 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 CHAPTER Examining the Petals Around the Rose Game The Petals Around the Rose game, featured in Figure 3.1, illustrates all the new skills you learn in this chapter. The premise of the Petals game is very simple. The computer rolls a set of five dice and asks the user to guess the number of petals around the rose. The user enters a number and presses the button. The computer indicates whether this value is correct, and provides a new set of dice. Once the user understands the secret, it’s a very easy game, but it can take a long time to figure out how it works. Try the game before you know how it’s done. Creating a Random Number The dice game, like many other games, relies on random number generation to make things interesting. Most programming languages have at least one way to cre- ate random numbers. PHP’s rand function makes it easy to create random numbers. 56 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 3.1 This is a new twist on an old dice puzzle. Viewing the Roll Em Program The Roll Em program shown in Figure 3.2 demonstrates how the rand function can generate virtual dice. The code for the Roll Em program shows how easy random number generation is: <html> <head> <title>Roll Em!</title> </head> <body> <h1>Roll Em!</h1> <h3>Demonstrates rolling a die</h3> <? $roll = rand(1,6); print “You rolled a $roll”; print “<br>”; print “<img src = die$roll.jpg>”; ?> <br> Refresh this page in the browser to roll another die. </body> </html> 57 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 FIGURE 3.2 The die roll is randomly generated by PHP . of the story .php code is plain HTML. The only part that’s in PHP is one long print statement, which uses the print <<<HERE syntax to print a long line of HTML text with PHP variables. some incredibly important concepts: what variables are, and how to create them in PHP; how to connect a form to a PHP program with modifications to the form’s method and action attributes; and. with the variables incorporated in the appropriate places. Here’s the code for the finished story .php page: <html> <head> <title>Little Boy Who?</title> </head> <body> <center> <h1>Little

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

TỪ KHÓA LIÊN QUAN