PHP 5/MySQL Programming- P90 potx

5 195 0
PHP 5/MySQL Programming- P90 potx

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

Thông tin tài liệu

global $dbConn; $output = “”; //process a query just to get field names $query = “SELECT * FROM $tableName”; $result = mysql_query($query, $dbConn); $output .= <<<HERE <form action = “processAdd.php” method = “post”> <table border = “1”> <tr> <th>Field</th> <th>Value</th> </tr> HERE; $fieldNum = 0; while ($theField = mysql_fetch_field($result)){ $fieldName = $theField->name; if ($fieldNum == 0){ //it’s the primary key field. It’ll be autoNumber $output .= <<<HERE <tr> <td>$fieldName</td> <td>AUTONUMBER <input type = “hidden” name = “$fieldName” value = “null”> </td> </tr> HERE; } else if (preg_match(“/(.*)ID$/”, $fieldName, $match)) { //it’s a foreign key reference. Use fieldToList to get //a select object for this field $valList = fieldToList($match[1],$fieldName, 0, “name”); $output .= <<<HERE 423 C h a p t e r 1 2B u i l d i n g a T h r e e -T i e r e d D a t a A p p l i c a t i o n <tr> <td>$fieldName</td> <td>$valList</td> </tr> HERE; } else { //it’s an ordinary field. Print a text box $output .= <<<HERE <tr> <td>$fieldName</td> <td><input type = “text” name = “$fieldName” value = “”> </td> </tr> HERE; } // end if $fieldNum++; } // end while $output .= <<<HERE <tr> <td colspan = 2> <input type = “hidden” name = “tableName” value = “$tableName”> <input type = “submit” value = “add record”> </td> </tr> </table> </form> HERE; return $output; } // end tToAdd 424 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 The INSERT statement that this function creates uses NULL as the primary key value, because all tables in the system are set to AUTO_INCREMENT. I used the same regular expression trick as in smartRToEdit() to recognize foreign key references. If they exist, I built a drop-down list with fieldToList() to display all possible val- ues for that field and send an appropriate key. Any field not recognized as a primary or foreign key will have an ordinary textbox. Processing an Added Record The tToAdd() function sends its results to processAdd.php, which reorganizes the data much like updateRecord.php. The field names and values are converted to arrays, which are passed to the procAdd() function. function procAdd($tableName, $fields, $vals){ //generates INSERT query, applies to database global $dbConn; $output = “”; $query = “INSERT into $tableName VALUES (“; foreach ($vals as $theValue){ $query .= “‘$theValue’, “; } // end foreach //trim off trailing space and comma $query = substr($query, 0, strlen($query) - 2); $query .= “)”; $output = “query is $query<br>\n”; $result = mysql_query($query, $dbConn); if ($result){ $output .= “<h3>Record added</h3>\n”; } else { $output .= “<h3>There was an error</h3>\n”; } // end if return $output; } // end procAdd The main job of procAdd() is to build an SQL INSERT statement using the results of tToAdd(). This insert is passed to the database and the user receives a report about the insertion attempt’s outcome. 425 C h a p t e r 1 2B u i l d i n g a T h r e e -T i e r e d D a t a A p p l i c a t i o n Building a List Box from a Field Both smartRToEdit() and tToAdd() need drop-down HTML lists following a specific pattern. In both cases, I needed to build a list that allows the user to select a key value based on some other field in the record. This list should be set so any value in the list can be indicated as the currently selected value. The fieldToList() function takes four parameters and uses them to build exactly such a list. function fieldToList($tableName, $keyName, $keyVal, $fieldName){ //given table and field, generates an HTML select structure //named $keyName. values will be key field of table, but //text will come from the $fieldName value. //keyVal indicates which element is currently selected global $dbConn; $output = “”; $query = “SELECT $keyName, $fieldName FROM $tableName”; $result = mysql_query($query, $dbConn); $output .= “<select name = $keyName>\n”; $recNum = 1; while ($row = mysql_fetch_assoc($result)){ $theIndex = $row[“$keyName”]; $theValue = $row[“$fieldName”]; $output .= <<<HERE right now, theIndex is $theIndex and keyVal is $keyVal <option value = “$theIndex” HERE; //make it currently selected item if ($theIndex == $keyVal){ $output .= “ selected”; } // end if $output .= “>$theValue</option>\n”; $recNum++; } // end while $output .= “</select>\n”; return $output; } // end fieldToList The fieldToList() function begins by generating a query that returns all records in the foreign table. I build an HTML SELECT object based on the results of this query. As I step through all records, I see if the current record corresponds to the $keyVal parameter. If so, that element is selected in the HTML. 426 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 Creating a Button That Returns Users to the Main Page To simplify navigation, I added a button at the end of each PHP program that returns the user to the program’s primary page. The mainButton() program cre- ates a very simple form calling whatever program is named in the $mainProgram variable, which is indicated at the top of the library. function mainButton(){ // creates a button to return to the main program global $mainProgram; $output .= <<<HERE <form action = “$mainProgram” method = “get”> <input type = “submit” value = “return to main screen”> </form> HERE; return $output; } // end mainButton Summary The details of the SpyMaster system can be dizzying, but the overall effect is a flex- ible design that you can easily update and modify. This system can accept modi- fications to the underlying database and can be adapted to an entirely different data set with relatively little effort. Although you didn’t learn any new PHP syntax in this chapter, you saw an exam- ple of coding for reuse and flexibility. You learned how to use include files to sim- plify coding of complex systems and how to build a library file with utility routines. You learned how to write code that can be adapted to multiple data sets and code that prevents certain kinds of user errors. You learned how to build pro- grams that help tie together relational data structures. The things you have learned in this chapter form the foundation of all data-enabled Web programming, which in turn form the backbone of e-commerce and content-management systems. 427 C h a p t e r 1 2B u i l d i n g a T h r e e -T i e r e d D a t a A p p l i c a t i o n . Added Record The tToAdd() function sends its results to processAdd .php, which reorganizes the data much like updateRecord .php. The field names and values are converted to arrays, which are passed. $tableName”; $result = mysql_query($query, $dbConn); $output .= <<<HERE <form action = “processAdd .php method = “post”> <table border = “1”> <tr> <th>Field</th> <th>Value</th> </tr> HERE; $fieldNum. That Returns Users to the Main Page To simplify navigation, I added a button at the end of each PHP program that returns the user to the program’s primary page. The mainButton() program cre- ates

Ngày đăng: 07/07/2014, 03: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

Tài liệu liên quan