PHP 5/MySQL Programming- P28 pdf

5 234 0
PHP 5/MySQL Programming- P28 pdf

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

Thông tin tài liệu

Building the Place Array I notice that each place is a string value associated with some number. I use the array() directive to preload the $place array with appropriate values. Zero has no corresponding place, so I simply left the 0 element blank. $place = array( “”, “on my thumb”, “on my shoe”, “on my knee”, “on a door”); Like most places in PHP, carriage returns don’t matter when you’re writing the source code. I put each place on a separate line, just because it looked neater that way. Writing Out the Lyrics The song itself is incredibly repetitive. Each verse is identical except for the verse number and place. For each verse, the value of the $verse variable is the current verse number. The corresponding place is stored in $place[$verse]. A large print statement in a for loop prints the entire code. //print out song for ($verse = 1; $verse <= 4; $verse++){ print <<<HERE This old man, He played $verse He played knick-knack $place[$verse] with a knick, knack, paddy-whack give a dog a bone This old man came rolling home HERE; } // end for loop The Fancy Old Man program illustrates very nicely the tradeoff associated with using arrays. Creating a program that uses arrays correctly often takes a little more planning than using control structures alone (as in This Old Man). However, the extra work up front pays off because the program is easier to modify and extend. 113 C h a p t e r 4 L o o p s a n d A r r a y s 114 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 Keeping Persistent Data Most traditional kinds of programming presume that the user and the program are engaging in a continual dialog. A program begins running, might ask the user some questions, responds to these inputs, and continues interacting with the user until he indicates an interest in leaving the program. Programs written on a Web server are different. The PHP programs you are writ- ing have an incredibly short life span. When the user makes a request to your PHP program through a Web browser, the server runs the PHP interpreter (the pro- gram that converts your PHP code into the underlying machine language your server understands). The result of the program is a Web page that is sent back to the user’s browser. Once your program sends a page to the user, the PHP program shuts down because its work is done. Web servers do not maintain contact with the browser after sending a page. Each request from the user is seen as an entirely new transaction. The Poker Dice program at the beginning of this chapter appears to interact with the user indefinitely. Actually, the same program is being called repeatedly. The program acts differently in different circumstances. Somehow it needs to keep track of what state it’s currently in. Counting with Form Fields You can store information a couple of ways, including files, XML, and databases. The second half of this book details these important ideas. The easiest approach to achieving data permanence is to hide the data in the user’s page. To illustrate, take a look at Figures 4.9 and 4.10. IN THE REAL WORLD The underlying Web protocol (HTTP) that Web servers use does not keep con- nections open any longer than necessary. This behavior is referred to as being a stateless protocol. Imagine if your program were kept running as long as any- body anywhere on the Web were looking at it. What if a person fired up your program and went to bed? Your Web server would have to maintain a connec- tion to that page all night. Also remember that your program might be called by thousands of people all at the same time. It can be very hard on your server to have all these concurrent connections open. Having stateless behavior improves your Web server’s performance, but that per- formance comes at a cost. Essentially, your programs have complete amnesia every time they run. You need a mechanism for determining the current state. Each time you click the Persistence program’s submit button, the counters incre- ment by one. The program behavior appears to contradict the basic nature of server-side programs because it seems to remember the previous counter value. In fact, if two users were accessing the Persistence program at the same time, each would count correctly. Look at the source code to see how it works: 115 C h a p t e r 4 L o o p s a n d A r r a y s FIGURE 4.9 The program has two counters that read 1 when the program is run the first time. FIGURE 4.10 Both values are incremented after the user clicks the submit button. <html> <head> <title> persistence demo </title> </head> <body> <h1>Persistence Demo</h1> <form> <? //increment the counters $txtBoxCounter++; $hdnCounter++; print <<<HERE <input type = “text” name = “txtBoxCounter” value = “$txtBoxCounter”> <input type = “hidden” name = “hdnCounter” value = “$hdnCounter”> <h3>The hidden value is $hdnCounter</h3> <input type = “submit” value = “click to increment counters”> HERE; ?> </form> </body> </html> Storing Data in the Text Box The program has two variables: $txtBoxCounter and $hdnCounter. For now, con- centrate on $txtBoxCounter, which is related to the text box. When the program 116 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 begins, it grabs the value of $txtBoxCounter (if it exists) and adds one to it. When the program prints the text box, it automatically places the $txtBoxCounter value in the text box. Since the form has no action attribute defined, the program automatically calls itself when the user clicks the submit button. This time, $txtBoxCounter has a value ( 1). When the program runs again, it increments $txtBoxCounter and stores the new value (now 2) in the text box. Each time the program runs, it stores in the text box the value it needs on the next run. Using a Hidden Field for Persistence The text box is convenient for this example because you can see it, but using a text box this way in real programs causes serious problems. Text boxes are editable by the user, which means she could insert any kind of information and really mess up your day. Hidden form fields are the unsung heroes of server-side programming. Look at $hdnCounter in the source code. This hidden field also has a counter, but the user never sees it. However, the value of the $hdnCounter variable is sent to the PHP program indicated by the form’s action attribute. That program can do anything with the attribute, including printing it in the HTML code body. Very often when you want to track information between pages, you store the information in hidden fields on the user’s page. The hidden fields technique shown here works fine for storing small amounts of information, but it is very inefficient and insecure when you are working with more serious forms of data. Writing the Poker Dice Program It’s time to take another look at the Poker Dice program that made its debut at the beginning of this chapter. As usual, this program doesn’t do anything you haven’t already learned. It is a little more complex than the trivial sample programs I show you in this chapter, but it’s surprisingly compact considering how much it does. It won’t surprise you that arrays and loops are the secret to this program’s success. Setting Up the HTML As always, a basic HTML page serves as the foundation for the PHP program. I add a simple style sheet to this page to make tan characters on a green background. TRAP 117 C h a p t e r 4 L o o p s a n d A r r a y s . different. The PHP programs you are writ- ing have an incredibly short life span. When the user makes a request to your PHP program through a Web browser, the server runs the PHP interpreter (the. blank. $place = array( “”, “on my thumb”, “on my shoe”, “on my knee”, “on a door”); Like most places in PHP, carriage returns don’t matter when you’re writing the source code. I put each place on a separate. PHP program through a Web browser, the server runs the PHP interpreter (the pro- gram that converts your PHP code into the underlying machine language your server understands). The result of the program

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

Từ khóa liên quan

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