PHP 5/MySQL Programming- P75 pot

5 127 0
PHP 5/MySQL Programming- P75 pot

Đang tải... (xem toàn văn)

Thông tin tài liệu

3. 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 $room variable 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.php without 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 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 FIGURE 10.2 The listSegments.php program lists all the data and allows the user to choose a record for editing. The code for the listSegments.php program is actually quite similar to the showAdventure.php program 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 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 HERE; } // 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 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 It’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 editSegment page is another form. When the user submits this form, control is passed to one more program, which actually modifies the database. The code for editSegment is 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 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 FIGURE 10.3 The editSegment.php program displays data from a requested record and lets the user manipulate that data. width: 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 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 . 10.2 The listSegments .php program lists all the data and allows the user to choose a record for editing. The code for the listSegments .php program is actually quite similar to the showAdventure .php program. 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 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 It’s. data structure, but then all you do is provide a link to the first record (by calling showSegment .php without any parameters). The program will keep calling itself. Viewing and Selecting Records I

Ngày đăng: 07/07/2014, 02:20

Mục lục

    PHP 5 / MySQL Programming for the Absolute Beginner

    Chapter 1: Exploring the PHP Environment

    Chapter 2: Using Variables and Input

    Chapter 3: Controlling Your Code with Conditions and Functions

    Chapter 4: Loops and Arrays

    Chapter 5: Better Arrays and String Handling

    Chapter 6: Working with Files

    Chapter 7: Writing Programs with Objects

    Chapter 8: XML and Content Management Systems

    Chapter 9: Using MySQL to Create Databases

Tài liệu cùng người dùng

Tài liệu liên quan