As you may have guessed, the next item on the task list is to build that showtopic.php file, to show the topic's postings. Listing 21.4 does just that. In this listing, lines 36 check for the existence of a value for topic_id in the GET query string. Because we intend to show all the posts within a selected topic, we need to know which topic to use in our query, and this is the manner in which the information is given to us. If a value in $_GET["topic_id"] does not exist, the user is redirected back to the topic listing page, presumably to try again. If the script makes it past the check for $_GET["topic_id"], line 9 makes the connection to the database, in preparation for issuing queries. Listing 21.4. Script to Show Topic Posts
Lines 1215 show the first of these queries, and this one is used to validate that the topic_id sent in the query string is actually a valid entry by selecting the associated topic_title for the topic in question. If the validation fails the test in line 17, a message is created in lines 1920, and the script breaks out of the if...else statement and finishes up by printing HTML. This output looks like Figure 21.6. Figure 21.6. Invalid topic selected.If, however, the topic is valid, we extract the value of topic_title in line 24, again using stripslashes() to remove any escape characters. Next, a query is issued in lines 2834 to gather all the posts associated with that topic in ascending order by time. In this case, newest posts are at the bottom of the list. At line 37, a block of text is started, containing the beginnings of an HTML table. Lines 3843 set up a table with two columns: one for the author of the post and one for the post text itself. We stop writing the text block momentarily, and at line 45 we begin to loop through the results of the original query. The while loop in line 45 says that although there are elements to be extracted from the resultset, extract each row as an array called $posts_info, and use the field names as the array element to assign the value to a new variable. So, the first element we try to extract is the post_id field on line 46. We assign the value of $posts_info['post_id'] to the variable $post_id, meaning that we get a local value for $post_id from an array called $posts_info, containing a field called post_id. Continue doing this for the $post_text, $post_create_time, and $post_owner variables in lines 4749. The stripslashes() function is again used to remove any escape characters, and the nl2br() function is used on the value of $posts_info[post_text] to replace all newline characters with XHTML-compliant line break characters. In line 51, we continue to write to the $display_block string, using the concatenation operator (.=) to make sure that this string is tacked on to the end of the string we have created so far. In line 53, we create the HTML table column to display the author and creation time of the post. The second HTML table row, on line 55, shows the text of the post as well as a link to reply to the post. On line 60, we break out of the while loop, and on line 70 add the last bit to the $display_block string to close the table. The remaining lines print the HTML for the page, including the value of the $display_block string. If you save this file as showtopic.php and place it in your web server document root, and if you have posts in your database tables, you may see something like Figure 21.7. Figure 21.7. Posts in a topic.A one-post topic is boring, so let's finish up this chapter by creating the script to add a post to a topic. |