This function’s job is to return an HTML list box containing the room names of every segment in the database.. The list box is set up so that whatever room number is associated with the
Trang 1value = “$roomNum”>
</td>
</tr>
<tr>
<td></td>
<td>$northList</td>
<td></td>
</tr>
<tr>
<td>$westList</td>
<td>
<textarea rows = 5 cols = 30 name = “description”>$theText</textarea>
</td>
<td>$eastList</td>
</tr>
<tr>
<td></td>
<td>$southList</td>
<td></td>
</tr>
<tr>
<td colspan = 3>
<input type = “submit”
value = “save this room”>
</td>
</table>
</form>
HERE;
function makeList($dir, $current){
//make a list of all the places in the system
353
i t
Trang 2global $conn;
$listCode = “<select name = $dir>\n”;
$sql = “SELECT id, name FROM adventure”;
$result = mysql_query($sql);
$rowNum = 0;
while ($row = mysql_fetch_assoc($result)){
$id = $row[“id”];
$placeName = $row[“name”];
$listCode = “ <option value = $id\n”;
//select this option if it’s the one indicated
if ($rowNum == $current){
$listCode = “ selected\n”;
} // end if
$listCode = “>$placeName</option>\n”;
$rowNum++;
} // end while
return $listCode;
} // end makeList
?>
</body>
</html>
Generating Variables
After the standard database connection, the code creates a number of variables
function This function’s job is to return an HTML list box containing the room names of every segment in the database The list box is set up so that whatever room number is associated with the indicated field is the default
Printing the HTML Code
The central part of the program consists of a large print statement that develops the HTML code The code in this case is a large table enclosed in a form Every field
in the record has a form element associated with it When the user submits this form, it should have all the data necessary to update a record in the database
354
g r
s o
l u
g in
e r
Trang 3The one element the user should not be able to directly edit is the room number.
This is stored in a hidden field The directional room numbers are encoded in the
list boxes All other data is in appropriately named text boxes
Creating the List Boxes
The list boxes require a little bit of thought to construct
The makeList()function expects two parameters The $dirparameter holds the
infor-mation about which room is currently selected for this particular field of the
main program
The function makes a query to the database to request all the room names Each
name is added to the list box code at the appropriate time with the
correspond-ing numeric value Whenever the record number corresponds to the current
value of the record, HTML code specifies that this should be the selected item in
the list box
Committing Changes to the Database
program I won’t repeat the screen shot for this program, because the visuals are
unimportant However, this program actually updates the database with
what-ever values the user has chosen
<head>
<title>SaveRoom.php</title>
</head>
<body>
<?
//Once a room has been edited by editSegment, this program
//updates the database accordingly.
//connect to database
$conn = mysql_connect(“localhost”, “”, “”);
$select = mysql_select_db(“chapter7”, $conn);
355
i t
Trang 4$sql = <<<HERE
UPDATE adventure
SET
name = ‘$name’,
description = ‘$description’,
north = $north,
east = $east,
south = $south,
west = $west
WHERE
id = $id
HERE;
//print $sql;
$result = mysql_query($sql);
if ($result){
print “<h3>$name room updated successfully</h3>\n”;
print “<a href = \”listSegments.php\”>view the rooms</a>\n”;
} else {
print “<h3>There was a problem with the database</h3>\n”;
} // end if
?>
</body>
</html>
This program begins with standard data connections It then constructs an
UPDATE SQLstatement The statement is quite simple, because all the work is done
in the previous program I then simply applied the query to the database and
command If the update request was successful, I let the user know and provide
very helpful) feedback to the user
Summary
In this chapter you begin using external programs to manage data You learn how MySQL can interpret basic SQL statements for defining and manipulating data
356
g r
s o
l u
g in
e r
Trang 5You create a database directly in the MySQL console, and you also learn how to
build and manipulate databases with SQLyog You combine these skills to create
an interesting and expandable game
357
i t
C H A L L E N G E S
1 Add a new room command to the adventure generator Hint: Think about
how I created a new test in the quiz machine program from chapter 6.
2 Write PHP programs to view, add, and edit records in the phone list.
3 Write a program that asks a user’s name and searches the database for
that user.
4 Create a front end for another simple database.