This simple program established the connection and ensured that everything was
stored as I expected. Whenever I write a data program, I usually write something
like this that quickly steps through my data to ensure everything is working cor-
rectly. There’s no point in moving on until you know you have the basic connection.
I did not give you a screenshot of this program because it isn’t very pretty, but I
did include it on the CD-ROM so you can run it yourself. The point here is to start
small and then turn your basic program into something more sophisticated one
step at a time.
Displaying One Segment
The actual gameplay consists of repeated calls to the showSegment.php program.
This program takes a segment ID as its one input and then uses that data to build
a page based on that database’s record. The only surprise is how simple the code
is for this program.
<html>
<head>
<title>Show Segment</title>
<style type = “text/css”>
body {
color:red
}
td {
color: white;
background-color: blue;
width: 20%;
height: 3em;
font-size: 20pt
}
</style>
</head>
<body>
<?
if (empty($room)){
$room = 1;
} // end if
//connect to database
$conn = mysql_connect(“localhost”, “”, “”);
343
C
h
a
p
t
e
r
1
0
C
o
n
n
e
c
t
i
n
g
t
o
D
a
t
a
b
a
s
e
s
w
i
t
h
i
n
P
H
P
$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”];
$northButton = buildButton(“north”);
$eastButton = buildButton(“east”);
$westButton = buildButton(“west”);
$southButton = buildButton(“south”);
$roomName = $mainRow[“name”];
print <<<HERE
<center><h1>$roomName</h1></center>
<form method = “post”>
<table border = 1>
<tr>
<td></td>
<td>$northButton</td>
<td></td>
</tr>
<tr>
<td>$eastButton</td>
<td>$theText</td>
<td>$westButton</td>
</tr>
<tr>
<td></td>
<td>$southButton</td>
<td></td>
</tr>
</table>
<center>
<input type = “submit”
value = “go”>
</center>
</form>
344
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
HERE;
function buildButton($dir){
//builds a button for the specified direction
global $mainRow, $conn;
$newID = $mainRow[$dir];
//print “newID is $newID”;
$query = “SELECT name FROM adventure WHERE id = $newID”;
$result = mysql_query($query, $conn);
$row = mysql_fetch_assoc($result);
$roomName = $row[“name”];
$buttonText = <<< HERE
<input type = “radio”
name = “room”
value = “$newID”>$roomName
HERE;
return $buttonText;
} // end build button
?>
</body>
</html>
Creating a CSS Style
I began the HTML with a cascading style sheet (CSS) style. My program is visually
unappealing, but placing a CSS style here is the answer to my visual design dis-
ability. All I need to do is get somebody with an actual sense of style to clean up
my CSS and I have a good-looking page.
Making the Data Connection
As usual, the program begins with some housekeeping. If the user hasn’t specif-
ically chosen a segment number, the program placed him in room number one,
which is designated as the starting room. If the user doesn’t specify a value, the
default action is a program crash, because it won’t know in what room to place
the user. I added a default so if this happens, the program assumes it’s a new
adventure and starts at the beginning.
345
C
h
a
p
t
e
r
1
0
C
o
n
n
e
c
t
i
n
g
t
o
D
a
t
a
b
a
s
e
s
w
i
t
h
i
n
P
H
P
if (empty($room)){
$room = 1;
} // 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”];
I then make an ordinary connection to the database and choose the record per-
taining to the current room number. That query is stored in the
$mainRow variable
as an associative array.
Generating Variables for the Code
Most of the program writes the HTML for the current record to the screen. To
make things simple, I create some variables for anything that might be tricky.
$theText = $mainRow[“description”];
$roomName = $mainRow[“name”];
$northButton = buildButton(“north”);
$eastButton = buildButton(“east”);
$westButton = buildButton(“west”);
$southButton = buildButton(“south”);
I stored the description field of the current row into a variable named $theText. I
made a similar variable for the room name.
The button variables are a little different. I decided to create an HTML option but-
ton to represent each of the places the user could go. I use a custom function
called
buildButton() to make each button.
346
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
IN THE REAL WORLD
It isn’t strictly necessary to store the description field in a variable, but I inter-
polate this value into HTML code. I’ve found that interpolating associative array
values can be a little tricky. In general, I like to copy an associative value to some
temporary variable if I’m going to interpolate it. It’s just a lot easier that way.
Writing the buildButton() Function
The procedure for building the buttons was repetitive enough to warrant a func-
tion. Each button is a radio button corresponding to a direction. The radio button
will have a value that comes from the corresponding direction value from the cur-
rent record. If the
north field of the current record is 12 (meaning if the user goes
North load the data in record 12), the radio button’s value should be 12.
The trickier thing is getting the appropriate label. The next room’s ID is all that’s
stored in the current record. If you want to display the room’s name, you must
make another query to the database. That’s exactly what the
buildButton() func-
tion does:
function buildButton($dir){
//builds a button for the specified direction
global $mainRow, $conn;
$newID = $mainRow[$dir];
//print “newID is $newID”;
$query = “SELECT name FROM adventure WHERE id = $newID”;
$result = mysql_query($query, $conn);
$row = mysql_fetch_assoc($result);
$roomName = $row[“name”];
$buttonText = <<< HERE
<input type = “radio”
name = “room”
value = “$newID”>$roomName
HERE;
return $buttonText;
} // end build button
The function follows these steps:
1. Borrows the
$mainRow array (which holds the value of the main record this
page is about) and the data connection in
$conn.
2. Pulls the ID for this button from the
$mainRow array and stores it in a local
variable. The
buildButton() function requires a direction name sent as a
parameter. This direction should be the field name for one of the direction
fields.
347
C
h
a
p
t
e
r
1
0
C
o
n
n
e
c
t
i
n
g
t
o
D
a
t
a
b
a
s
e
s
w
i
t
h
i
n
P
H
P