Displaying the Posts in a Topic


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

 1:  <?php 2:  //check for required info from the query string 3:  if (!isset($_GET["topic_id"])) { 4:      header("Location: topiclist.php"); 5:      exit; 6:  } 7: 8:  //connect to server 9:  $mysqli = mysqli_connect("localhost", "joeuser", "somepass", "testDB"); 10: 11: //verify the topic exists 12: $verify_topic_sql = "SELECT topic_title FROM forum_topics 13;                     WHERE topic_id = '".$_GET["topic_id"]."'"; 14: $verify_topic_res = mysqli_query($mysqli, $verify_topic_sql) 15:                     or die(mysqli_error($mysqli)); 16: 17: if (mysqli_num_rows($verify_topic_res) < 1) { 18:    //this topic does not exist 19:    $display_block = "<p><em>You have selected an invalid topic.<br/> 20:    Please <a href=\"topiclist.php\">try again</a>.</em></p>"; 21: } else { 22:    //get the topic title 23:    while ($topic_info = mysqli_fetch_array($verify_topic_res)) { 24:        $topic_title = stripslashes($topic_info['topic_title']); 25:    } 26: 27:    //gather the posts 28:    $get_posts_sql = "SELECT post_id, post_text,                         DATE_FORMAT(post_create_time, 29:                     '%b %e %Y at %r') AS fmt_post_create_time, post_owner 30:                     FROM forum_posts 31:                     WHERE topic_id = '".$_GET["topic_id"]."' 32:                     ORDER BY post_create_time ASC"; 33:    $get_posts_res = mysqli_query($mysqli, $get_posts_sql) 34:                    or die(mysqli_error($mysqli)); 35: 36:     //create the display string 37:     $display_block = " 38:     <p>Showing posts for the <strong>".$topic_title."</strong> topic:</p> 39:     <table width=\"100%\" cellpadding=\"3\" cellspacing=\"1\" border=\"1\"> 40:     <tr> 41:     <th>AUTHOR</th> 42:     <th>POST</th> 43:     </tr>"; 44: 45:     while ($posts_info = mysqli_fetch_array($get_posts_res)) { 46:         $post_id = $posts_info['post_id']; 47:         $post_text = nl2br(stripslashes($posts_info['post_text'])); 48:         $post_create_time = $posts_info['fmt_post_create_time']; 49:         $post_owner = stripslashes($posts_info['post_owner']); 50: 51:         //add to display 51:         $display_block .= " 52:         <tr> 53:         <td width=\"35%\" valign=\"top\">".$post_owner."<br/> 54:         [".$post_create_time."]</td> 55:         <td width=\"65%\" valign=\"top\">".$post_text."<br/><br/> 56:         <a href=\"replytopost.php?post_\"> 57:         <strong>REPLY TO POST</strong></a></td> 59:         </tr>"; 60:     } 61: 62:     //free results 63:     mysqli_free_result($get_posts_res); 64:     mysqli_free_result($verify_topic_res); 65: 66:     //close connection to MySQL 67:     mysqli_close($mysqli); 68: 69:     //close up the table 70:     $display_block .= "</table>"; 71: } 72: ?> 73: <html> 74: <head> 75: <title>Posts in Topic</title> 76: </head> 77: <body> 78: <h1>Posts in Topic</h1> 79: <?php echo $display_block; ?> 80: </body> 81: </html>

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.




Sams Teach Yourself PHP, MySQL And Apache All in One
Sams Teach Yourself PHP, MySQL and Apache All in One (3rd Edition)
ISBN: 0672328739
EAN: 2147483647
Year: 2004
Pages: 327

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net