$fieldName $valList HERE; } else { //it’s an ordinary field.. The INSERT statement that this function creates uses NULL as the primary keyval-ues for that field and send an appropriate
Trang 1global $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
i l
i o
Trang 2<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
g r
s o
l u
g in
e r
Trang 3The INSERT statement that this function creates uses NULL as the primary key
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
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
of tToAdd() This insert is passed to the database and the user receives a report
about the insertion attempt’s outcome.
425
i l
i o
Trang 4Building a List Box from a Field
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
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
query 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.
426
g r
s o
l u
g in
e r
Trang 5Creating a Button That
Returns Users to the Main Page
To simplify navigation, I added a button at the end of each PHP program that
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
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
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
i l
i o