PHP 5/MySQL Programming- P34 ppt

5 246 0
PHP 5/MySQL Programming- P34 ppt

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

Thông tin tài liệu

All the fields sent to your program are automatically stored in a special associa- tive array called $_REQUEST. Each field name on the original form becomes a key, and the value of that field becomes the value associated with that key. If you have a form with a field called userName, you can get the value of the field by calling $_REQUEST[“userName”]. The $_REQUEST array is also useful because you can use a foreach loop to quickly determine the names and values of all form elements known to the program. The formReader.php program source code illustrates how this is done: <!doctype html public “-//W3C//DTD HTML 4.0 //EN”> <html> <head> <title>Form Reader</title> </head> <body> <h1>Form Reader</h1> <h3>Here are the fields I found on the form</h3> <? print <<<HERE <table border = 1> <tr> <th>Field</th> <th>Value</th> </tr> HERE; foreach ($_REQUEST as $field => $value){ print <<<HERE <tr> <td>$field</td> <td>$value</td> </tr> HERE; } // end foreach print “</table>\n”; ?> </body> </html> 143 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 Note how I stepped through the $_REQUEST array. Each time through the foreach loop, the current field name is stored in the $field variable and the value of that field is stored in $value. I use this script when I’m debugging my programs. If I’m not getting the form elements I expected from a form, I put a foreach $_REQUEST loop in at the top of my program to make sure I know exactly what’s being sent to the program. Often this type of procedure can help you find misspellings or other bugs. Creating a Multidimensional Array Arrays are very useful structures for storing various kinds of data into the com- puter’s memory. Normal arrays are much like lists. Associative arrays are like name/value pairs. A third special type, a multidimensional array , acts much like table data. For instance, imagine you were trying to write a program to help users determine the distance between major cities. You might start on paper with a table like Table 5.1. TRICK 144 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 Indianapolis New York Tokyo London Indianapolis 0 648 6476 4000 New York 648 0 6760 3470 Tokyo 6476 6760 0 5956 London 4000 3470 5956 0 TABLE 5.1 DISTANCES B ETWEEN M AJOR C ITIES IN THE REAL WORLD PHP provides some other variables related to $_REQUEST. The $HTTP_POST_VARS array holds all the names and values sent through a POST request, and $HTTP_GET_VARS array holds names and values sent through a get request. You can use this feature to make your code more secure. If you create variables only from the $HTTP_POST_VARS array, for example, all input sent via the get method are ignored. This makes it harder for users to forge data by putting field names in the browser’s address bar. Of course, a clever user can still write a form that contains bogus fields, so you always have to be a little suspicious whenever you get any data from the user. It’s reasonably common to work with this sort of tabular data in a computer pro- gram. PHP (and most languages) provides a special type of array to assist in work- ing with this kind of information. The basicMultiArray program featured in Figures 5.8 and 5.9 illustrates how a program can encapsulate a table. 145 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 FIGURE 5.8 The user can choose origin and destination cities from select groups. FIGURE 5.9 The program looks up the distance between the cities and returns an appropriate value. Building the HTML for the Basic Multidimensional Array Using a two-dimensional array is pretty easy if you plan well. I first wrote out my table on paper. (Actually, I have a write-on, wipe-off board in my office for exactly this kind of situation.) I assigned a numeric value to each city: Indianapolis = 0 New York = 1 Tokyo = 2 London = 3 This makes it easier to track the cities later on. The HTML code builds the two select boxes and a submit button in a form. <!doctype html public “-//W3C//DTD HTML 4.0 //EN”> <html> <head> <title>Basic multi-dimensional array</title> </head> <body> <h1>Basic 2D Array</h1> <form action = basicMultiArray.php> <table border = 1> <tr> <th>First city</th> <th>Second city</th> <tr> <!— note each option value is numeric —> <tr> <td> <select name = “cityA”> <option value = 0>Indianapolis</option> <option value = 1>New York</option> <option value = 2>Tokyo</option> <option value = 3>London</option> </select> </td> 146 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 <td> <select name = “cityB”> <option value = 0>Indianapolis</option> <option value = 1>New York</option> <option value = 2>Tokyo</option> <option value = 3>London</option> </select> </td> </tr> <tr> <td colspan = 2> <input type = “submit” value = “calculate distance”> </td> </tr> </table> </body> </html> Recall that when the user submits this form, it sends two variables. The cityA variable contains the value property associated with whatever city the user selected; cityB likewise contains the value of the currently selected destination city. I carefully set up the value properties so they coordinate with each city’s numeric index. If the user chooses New York as the origin city, the value of $cityA is 1, because I decided that New York would be represented by the value 1. I’m giv- ing numeric values because the information is all stored in arrays, and normal arrays take numeric indices. (In the next section I show you how to do the same thing with associative arrays.) Responding to the Distance Query The PHP code that determines the distance between cities is actually quite simple once the arrays are in place: <!doctype html public “-//W3C//DTD HTML 4.0 //EN”> <html> <head> <title>Distance calculator</title> </head> <body> 147 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 . quickly determine the names and values of all form elements known to the program. The formReader .php program source code illustrates how this is done: <!doctype html public “-//W3C//DTD HTML. 6760 0 5956 London 4000 3470 5956 0 TABLE 5.1 DISTANCES B ETWEEN M AJOR C ITIES IN THE REAL WORLD PHP provides some other variables related to $_REQUEST. The $HTTP_POST_VARS array holds all the. the user. It’s reasonably common to work with this sort of tabular data in a computer pro- gram. PHP (and most languages) provides a special type of array to assist in work- ing with this kind

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