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

PHP 5/MySQL Programming- P41 doc

5 230 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 secret is to store the current answer key in a hidden form element and pass this element to another program. I created a form with two hidden fields. I stored the name of the puzzle in a field called puzzleName and the entire HTML of the answer key in a field called key. When the user presses the submit key, it calls wordFindKey. Printing the Answer Key The wordFindKey program is very simplistic, because all the work of generating the answer key was done by the Word Search program. wordFindKey has only to retrieve the puzzle name and answer key from form variables and print them out. Since the key has even been formatted as a table, the wordFindKey program needn’t do any heavy lifting. <!doctype html public “-//W3C//DTD HTML 4.0 //EN”> <html> <head> <title>Word Find Answer Key</title> </head> <body> <? //answer key for word find //called from wordFind.php 178 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 IN THE REAL WORLD Passing the answer key to another program is a kind of dirty trick. It works for a couple of reasons. First, since the key field is hidden and the form sends data through the post method, the user is unlikely to know that the answer to the puz- zle is literally under his nose. Since I expect this program mainly to be used by teachers who would print the puzzle anyway, this is fine. Even without the secrecy concerns, it is necessary to pass the key data by post because it is longer than the 256 characters allowed by the get method. Sending the HTML-formatted answer key to the next program made the second program quite simple, but there is another advantage to this approach: It is dif- ficult to send entire arrays through form variables. However, by creating the HTML table, all the array data was reduced to one string value, which can be passed to another program through a form. print <<<HERE <center> <h1>$puzzleName Answer key</h1> $key </center> HERE; ?> </body> </html> Summary In this chapter you see how important it is to put together data in meaningful ways. You look at a number of more powerful arrays and tools to manipulate them. You learn how to use the foreach loop to look at each element of an array in turn. You can use string indices to generate associative arrays and make two- dimensional arrays using both numeric and string indices. You learn how to do several kinds of string-manipulation tricks, including searching for one string inside another, extracting substrings, and splitting a string into an array. You put all these skills together in an interesting and detailed application. You should be proud of your efforts. 179 C h a p t e r 5 B e t t e r A r r a y s a n d S t r i n g H a n d l i n g CHALLENGES 1. Add the ability to use diagonals in your puzzles. (Hint: You need only combine the formulas I established. You don’t need any new ones.) 2. Create a game of Battle Ship for two players on the same computer. The game prints a grid. (Preset the fleet locations to make it easier.) Let the user choose a location on the grid via a checkbox. Report the result of his firing back and then give the screen to the second user. 3. Write a version of Conway’s Life. This program simulates cellular life on a grid with three simple rules. a. Each cell with exactly three neighbors becomes or remains alive. b. Each cell currently alive with exactly two neighbors remains alive. c. All other cells die off. 4. Randomly generate the first cell and let the user press a button to generate the next generation. This page intentionally left blank A s your experience in programming grows, the relative importance of data becomes increasingly apparent. You began your understanding of data with simple variables, but learned how simple and more complex arrays can make your programs more flexible and more powerful. However, data stored in the computer’s memory is transient, especially in the server environment. It is often necessary to store information in a form that is more permanent than the constructs you have learned so far. PHP provides a number of powerful functions for working with text files. With these skills, you create extremely useful programs. Specifically, you learn how to: • Open files for read, write, and append access • Use file handles to manipulate text files • Write data to a text file • Read data from a text file • Open an entire file into an array • Modify text data on-the-fly • Get information about all the files in a particular directory • Get a subset of files based on filenames W o r k i n g w i t h F i l e s 6 CHAPTER 182 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 Previewing the Quiz Machine This chapter’s main program is a fun and powerful tool that you can use in many different ways. It is not simply one program, but a system of programs that work together to let you automatically create, administer, and grade multiple-choice quizzes. Entering the Quiz Machine System Figure 6.1 shows the system’s main page. The user needs a password to take a test and an administrator password to edit a test. In this case, I entered the adminis- trative password (it’s absolute—like in Absolute Beginner’s Guide ) into the appro- priate password box, and I’m going to edit the Monty Python quiz. I refer to the quiz machine as a system rather than a program because it uses a number of programs intended to work together. Editing a Quiz The screen shown in Figure 6.2 appears when the user has the correct password. You can see the requested quiz in a special format on the screen. The quiz administrator can edit the quiz. Each quiz has a name, instructor e-mail address, and password. Each question is stored in a single line with the question, four possible answers, and the correct answer separated by colon ( :) characters. TRICK IN THE REAL WORLD It is reasonably easy to build an HTML page that presents a quiz and a PHP program to grade only that quiz. However, if you want several quizzes, it might be worth the investment in time and energy to build a system that can automate the creation and administration of quizzes. The real power of programming comes into play not just when you solve one immediate problem, but when you can produce a solution that can be applied to an entire range of related problems. The quiz machine is an example of exactly such a system. It takes a little more effort to build, but the effort really pays off when you have a system to reuse. . has even been formatted as a table, the wordFindKey program needn’t do any heavy lifting. <!doctype html public “-//W3C//DTD HTML 4.0 //EN”> <html> <head> <title>Word. Key</title> </head> <body> <? //answer key for word find //called from wordFind .php 178 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 IN. store information in a form that is more permanent than the constructs you have learned so far. PHP provides a number of powerful functions for working with text files. With these skills, you

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

TỪ KHÓA LIÊN QUAN

w