Building a List Box from a FieldBoth smartRToEdit()and
October 31, 2006 on 7:29 pm | In php | No CommentsCreating a Button That Returns Users to the Main PageTo simplify navigation, I added a button at the end of each PHP program thatreturns the user to the program s primary page. The mainButton()program cre- ates a very simple form calling whatever program is named in the $mainProgramvariable, which is indicated at the top of the library. function mainButton(){ // creates a button to return to the main programglobal $mainProgram; $output .= <<
HERE; return $output; } // end mainButtonSummaryThe details of the SpyMastersystem 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 differentdata 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 includefiles to sim- plify coding of complex systems and how to build a library file with utilityroutines. You learned how to write code that can be adapted to multiple data setsand 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 havelearned 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.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services
Building a List Box from a FieldBoth smartRToEdit()and
October 31, 2006 on 7:29 pm | In php | No CommentsBuilding a List Box from a FieldBoth smartRToEdit()and tToAdd()need drop-down HTML lists following a specificpattern. In both cases, I needed to build a list that allows the user to select a keyvalue based on some other field in the record. This list should be set so any valuein 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 selectedglobal $dbConn; $output = ?; $query = SELECT $keyName, $fieldName FROM $tableName ?; $result = mysql_query($query, $dbConn); $output .=
\n ?; return $output; } // end fieldToListThe fieldToList()function begins by generating a query that returns all recordsin the foreign table. I build an HTML SELECTobject based on the results of thisquery. As I step through all records, I see if the current record corresponds to the$keyValparameter. If so, that element is selected in the HTML. 426PHP5/MySQLProgrammingfortheAbsoluteBeginner
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services
$fieldName $valList HERE; } else { //it s an
October 31, 2006 on 9:39 am | In php | No CommentsThe INSERTstatement that this function creates uses NULLas the primary keyvalue, because all tables in the system are set to AUTO_INCREMENT. I used the sameregular 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 RecordThe tToAdd()function sends its results to processAdd.php, which reorganizes thedata much like updateRecord.php. The field names and values are converted toarrays, which are passed to the procAdd()function. function procAdd($tableName, $fields, $vals){ //generates INSERT query, applies to databaseglobal $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
\n ?; $result = mysql_query($query, $dbConn); if ($result){ $output .=
Record added
\n ?; } else { $output .=
There was an error
\n ?; } // end ifreturn $output; } // end procAddThe main job of procAdd()is to build an SQL INSERTstatement using the resultsof tToAdd(). This insert is passed to the database and the user receives a reportabout the insertion attempt s outcome.
Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra servlet hosting services
$fieldName $valList HERE; } else { //it s an
October 31, 2006 on 9:39 am | In php | No Comments HERE; } else { //it s an ordinary field. Print a text box$output .= <<
HERE; } // end if$fieldNum++; } // end while$output .= <<
HERE; return $output; } // end tToAdd424PHP5/MySQLProgrammingfortheAbsoluteBeginner
Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra servlet hosting services
The primary job of updateRec()is to build an
October 30, 2006 on 9:26 pm | In php | No CommentsThe primary job of updateRec()is to build an SQL UPDATEstatement based on theparameters passed to it. It is expecting a table name, an array containing fieldnames, and another array containing field values. The UPDATEstatement is pri- marily a list of field names and values, which can be easily obtained with a forloop stepping through the $fieldsand $valsarrays. Once the query has been created, it is submitted to the database. The success orfailure of the update is reported back to the user. Deleting a RecordDeleting a record is actually pretty easy compared to adding or updating. Allthat s necessary is the table name, key field name, and key field value. ThedeleteRec()function accepts these parameters and uses them to build an SQLDELETEstatement. As usual, the success or failure of the operation is returned aspart of the output string. function delRec ($table, $keyName, $keyVal){ //deletes $keyVal record from $tableglobal $dbConn; $output = ?; $query = DELETE from $table WHERE $keyName = $keyVal ?; print query is $query
\n ?; $result = mysql_query($query, $dbConn); if ($result){ $output =
Record successfully deleted
\n ?; } else { $output =
Error deleting record
\n ?; } //end ifreturn $output; } // end delRecAdding a RecordAdding a new record is much like editing a record. It is a two-step process. Thefirst screen builds a page in which you can add a record. I used techniques fromthe smartRToEdit()function to ensure the primary and foreign key references areedited appropriately. function tToAdd($tableName){ //given table name, generates HTML form to add an entry to the//table. Works like smartRToEdit in recognizing foreign keys422PHP5/MySQLProgrammingfortheAbsoluteBeginner
Hint: If you are looking for high quality and reliable webspace provider to host and run your jsp hosting application check Virtualwebstudio jsp web hosting provider
The primary job of updateRec()is to build an
October 30, 2006 on 9:26 pm | In php | No Commentsglobal $dbConn; $output = ?; //process a query just to get field names$query = SELECT * FROM $tableName ?; $result = mysql_query($query, $dbConn); $output .= <<