PHP 5/MySQL Programming- P35 pps

5 174 0
PHP 5/MySQL Programming- P35 pps

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

Thông tin tài liệu

<? $city = array ( “Indianapolis”, “New York”, “Tokyo”, “London” ); $distance = array ( array (0, 648, 6476, 4000), array (648, 0, 6760, 3470), array (6476, 6760, 0, 5956), array (4000, 3470, 5956, 0) ); $result = $distance[$cityA][$cityB]; print “<h3>The distance between “; print “$city[$cityA] and $city[$cityB]”; print “ is $result miles.</h3>”; ?> </body> </html> Storing City Names in the $city Array I have two arrays in this program, $city and $distance. The $city array is a com- pletely normal array of string values. It contains a list of city names. I set up the array so the numeric values I assigned to the city would correspond to the index in this array. Remember that array indices usually start with 0, so Indianapolis is 0, New York is 1, and so on. The user won’t care that Indianapolis is city 0, so the $city array assigns names to the various cities. If the user chose city 0 (Indianapolis) for the $cityA field, I can refer to the name of that city as $city[$cityA] because $cityA contains the value 0 and $city[0] is Indianapolis. Storing Distances in the $distance Array The distances don’t fit into a regular list, because it requires two values to deter- mine a distance. You must know from which city you are coming and going to 148 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 calculate a distance. These two values correspond to rows and columns in the original table. Look again at the code that generates the $distance array: $distance = array ( array (0, 648, 6476, 4000), array (648, 0, 6760, 3470), array (6476, 6760, 0, 5956), array (4000, 3470, 5956, 0) ); The $distance array is actually an array full of other arrays! Each of the inner arrays corresponds to distance from a certain destination city. For example, since Indianapolis is city 0, the first (zeroth?) inner array refers to the distance between Indy and the other cities. If it helps, you can think of each inner array as a row of a table, and the table as an array of rows. It might sound complicated to build a two-dimensional array, but it is more nat- ural than you may think. If you compare the original data in Table 5.1 with the code that creates the two-dimensional array, you see that all the numbers are in the right place. No need to stop at two dimensions. It’s possible to build arrays with three, four, or any other number of dimensions. However, it becomes difficult to visualize how the data works with these complex arrays. Generally, one and two dimensions are as complex as ordinary arrays should get. For more complex data types, look toward file-manipulation tools and relational data structures, which you learn throughout the rest of this book. Getting Data from the $distance Array Once data is stored in a two-dimensional array, it is reasonably easy to retrieve. To look up information in a table, you need to know the row and column. A two-dimensional array requires two indices—one for the row and one for the column. To find the distance from Tokyo (city number 2) to New York (city number 1), sim- ply refer to $distance[2][1]. The code for the program gets the index values from the form: $result = $distance[$cityA][$cityB]; This value is stored in the variable $result and then sent to the user. TRICK 149 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 Making a Two-Dimensional Associative Array You can also create two-dimensional associative arrays. It takes a little more work to set it up, but can be worth it because the name/value relationship eliminates the need to track numeric identifiers for each element. Another version of the multiArray program illustrates how to use associative arrays to generate the same city-distance program. Since this program looks exactly like the basicMultiArray program to the user, I am not showing the screen shots. All of this program’s interesting features are in the source code. Building the HTML for the Associative Array The HTML page for this program’s associative version is much like the indexed version, except for one major difference. See if you can spot the difference in the source code: <!doctype html public “-//W3C//DTD HTML 4.0 //EN”> <html> <head> <title>2D Array</title> </head> <body> <h1>2D Array</h1> <form action = multiArray.php> <table border = 1> <tr> <th>First city</th> <th>Second city</th> <tr> <!— note each option value is a string —> <tr> <td> <select name = “cityA”> <option value = “Indianapolis”>Indianapolis</option> <option value = “New York”>New York</option> TRICK 150 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 <option value = “Tokyo”>Tokyo</option> <option value = “London”>London</option> </select> </td> <td> <select name = “cityB”> <option value = “Indianapolis”>Indianapolis</option> <option value = “New York”>New York</option> <option value = “Tokyo”>Tokyo</option> <option value = “London”>London</option> </select> </td> </tr> <tr> <td colspan = 2> <input type = “submit” value = “calculate distance”> </td> </tr> </table> </body> </html> The only difference between this HTML page and the last one is the value prop- erties of the select objects. In this case, the distance array is an associative array, so it does not have numeric indices. Since the indices can be text based, I send the actual city name as the value for $cityA and $cityB. Responding to the Query The code for the associative response is interesting, because it spends a lot of effort to build the fancy associative array. Once the array is created, it’s very easy to work with. <!doctype html public “-//W3C//DTD HTML 4.0 //EN”> <html> <head> <title>Distance Calculator</title> 151 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 </head> <body> <h1>Distance Calculator</h1> <? //create arrays $indy = array ( “Indianapolis” => 0, “New York” => 648, “Tokyo” => 6476, “London” => 4000 ); $ny = array ( “Indianapolis” =>648, “New York” => 0, “Tokyo” => 6760, “London” => 3470 ); $tokyo = array ( “Indianapolis” => 6476, “New York” => 6760, “Tokyo” => 0, “London” => 5956 ); $london = array ( “Indianapolis” => 4000, “New York” => 3470, “Tokyo” => 5956, “London” => 0 ); //set up master array $distance = array ( “Indianapolis” => $indy, “New York” => $ny, “Tokyo” => $tokyo, “London” => $london ); 152 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 . Array</title> </head> <body> <h1>2D Array</h1> <form action = multiArray .php& gt; <table border = 1> <tr> <th>First city</th> <th>Second city</th> <tr> <!—

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

Mục lục

  • PHP 5 / MySQL Programming for the Absolute Beginner

    • Cover

    • 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 12: Building a Three-Tiered Data Application

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

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

Tài liệu liên quan