The radio button is called room, so the next time this program is called, the $roomvariable corresponds to the user-selected radio button.. Finishing the HTML All that’s left is adding a
Trang 13 Repeats the query creation process, building a query that requests only the row associated with the new ID
4 Pulls the room name from that array Once that’s done, it’s easy to build the radio button text The radio button is called room, so the next time this program is called, the $roomvariable corresponds to the user-selected radio button
Finishing the HTML
All that’s left is adding a Submit button to the form and closing the form and HTML The amazing thing is, that’s all you need This code alone is enough to let the user play this game It takes some effort to set up the data structure, but then all you do is provide a link to the first record (by calling showSegment.phpwithout any parameters) The program will keep calling itself
Viewing and Selecting Records
I suppose you could stop there, because the game is working, but the really great thing about this structure is how flexible it is It doesn’t take much more work
to create an editor that lets you add and modify records
This actually requires a couple of PHP programs The first, shown in Figure 10.2, prints out a summary of the entire game and allows the user to edit any node
348
g r
s o
l u
g in
e r
FIGURE 10.2
The listSegments.php
program lists all the
data and allows the
user to choose a
record for editing.
Trang 2The code for the listSegments.php program is actually quite similar to the
showAdventure.phpprogram you saw before It’s simply cleaned up a bit to put the
data in tables and has a form to call an editor when the user selects a record to
modify
<html>
<head>
<title>List Segments</title>
<style type = “text/css”>
body {
color:red
}
td, th {
color: white;
background-color: blue;
}
</style>
</head>
<body>
<?
$conn = mysql_connect(“localhost”, “”, “”);
$select = mysql_select_db(“chapter7”, $conn);
$sql = “SELECT * FROM adventure”;
$result = mysql_query($sql);
print <<<HERE
<form action = “editSegment.php”
method = “post”>
HERE;
while ($row = mysql_fetch_assoc($result)){
print “<table border = 1 width = 80%>\n”;
foreach($row as $key=>$value){
//print “$key: $value<br>\n”;
$roomNum = $row[“id”];
print <<<HERE
<tr>
<th width = 10%>$key</th>
<td>$value</td>
</tr>
349
i t
Trang 3} // end foreach
print <<<HERE
<tr>
<td colspan = 2><center>
<input type = “radio”
name = “room”
value = “$roomNum”>
Edit this room
<input type = “submit”
value = “go”>
</center></td>
</tr>
</table><br>
HERE;
} // end while
?>
<center>
<input type = “submit”
value = “edit indicated room”>
</center>
</form>
</body>
</html>
The entire program is contained in a form, which calls editSegment.php when activated The program opens a data connection and pulls all elements from the database It builds an HTML table for each record Each table contains a radio but-ton called room, with the value of the current room number Each table also has
a copy of the Submit button so the user doesn’t have to scroll all the way to the bottom of the page to submit the form
Editing the Record
When the user has chosen a record from listSegments.php, the editSegment.php program (shown in Figure 10.3) swings into action
350
g r
s o
l u
g in
e r
Trang 4It’s important to understand that the editSegment program doesn’t actually
change the record in the database Instead, it pulls up a form containing the
requested record’s current values and allows the user to determine the new values
The editSegmentpage is another form When the user submits this form, control
is passed to one more program, which actually modifies the database The code
for editSegmentis very similar to the code that displays a segment in play mode
The primary difference is that all the record data goes into editable fields
Take a careful look at how the game developer can select a room to go into for
each position A drop-down menu shows all the existing room names This device
allows the game developer to work directly with room names even though the
database will be much more concerned with room numbers
<html>
<head>
<title>Edit Segment</title>
<style type = “text/css”>
body {
color:red
}
td {
color: white;
background-color: blue;
351
i t
FIGURE 10.3
The
editSegment.php
program displays
data from a
requested record
and lets the
user manipulate
that data.
Trang 5width: 20%;
height: 5em;
text-align: center;
}
</style>
</head>
<body>
<?
if (empty($room)){
$room = 0;
} // end if
//connect to database
$conn = mysql_connect(“localhost”, “”, “”);
$select = mysql_select_db(“chapter7”, $conn);
$sql = “SELECT * FROM adventure WHERE id = ‘$room’”;
$result = mysql_query($sql);
$mainRow = mysql_fetch_assoc($result);
$theText = $mainRow[“description”];
$roomName = $mainRow[“name”];
$northList = makeList(“north”, $mainRow[“north”]);
$westList = makeList(“west”, $mainRow[“west”]);
$eastList = makeList(“east”, $mainRow[“east”]);
$southList = makeList(“south”, $mainRow[“south”]);
$roomNum = $mainRow[“id”];
print <<<HERE
<form action = “saveRoom.php”
method = “post”>
<table border = 1>
<tr>
<td colspan = 3>
Room # $roomNum:
<input type = “text”
name = “name”
value = “$roomName”>
<input type = “hidden”
name = “id”
352
g r
s o
l u
g in
e r