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 20.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], lines 911 make the connection to the database, in preparation for issuing queries.

Listing 20.4. Script to Show Topic Posts
  1: <?php  2: //check for required info from the query string  3: if (!$_GET[topic_id]) {  4:    header("Location: topiclist.php");  5:    exit;  6: }  7:  8: //connect to server and select database  9: $conn = mysql_connect("localhost", "joeuser", "somepass") 10:     or die(mysql_error()); 11: mysql_select_db("testDB",$conn) or die(mysql_error()); 12: 13: //verify the topic exists 14: $verify_topic = "select topic_title from forum_topics where 15:     topic_id = $_GET[topic_id]"; 16: $verify_topic_res = mysql_query($verify_topic, $conn) 17:     or die(mysql_error()); 18: 19: if (mysql_num_rows($verify_topic_res) < 1) { 20:     //this topic does not exist 21:    $display_block = "<P><em>You have selected an invalid topic. 22:     Please <a href=\"topiclist.php\">try again</a>.</em></p>"; 23: } else { 24:     //get the topic title 25:    $topic_title = stripslashes(mysql_result($verify_topic_res,0, 26:           'topic_title')); 27: 28:     //gather the posts 29:     $get_posts = "select post_id, post_text, date_format(post_create_time, 30:           '%b %e %Y at %r') as fmt_post_create_time, post_owner from 31:           forum_posts where topic_id = $_GET[topic_id] 32:           order by post_create_time asc"; 33: 34:     $get_posts_res = mysql_query($get_posts,$conn) or die(mysql_error()); 35: 36:     //create the display string 37:     $display_block = " 38:     <P>Showing posts for the <strong>$topic_title</strong> topic:</p> 39: 40:     <table width=100% cellpadding=3 cellspacing=1 border=1> 41:     <tr> 42:     <th>AUTHOR</th> 43:     <th>POST</th> 44:     </tr>"; 45: 46:     while ($posts_info = mysql_fetch_array($get_posts_res)) { 47:         $post_id = $posts_info['post_id']; 48:         $post_text = nl2br(stripslashes($posts_info['post_text'])); 49:         $post_create_time = $posts_info['fmt_post_create_time']; 50:         $post_owner = stripslashes($posts_info['post_owner']); 51: 52:         //add to display 53:         $display_block .= " 54:         <tr> 55:         <td width=35% valign=top>$post_owner<br>[$post_create_time]</td> 56:         <td width=65% valign=top>$post_text<br><br> 57:         <a href=\"replytopost.php?post_id=$post_id\"><strong>REPLY TO 58:         POST</strong></a></td> 59:         </tr>"; 60:     } 61: 62:     //close up the table 63:     $display_block .= "</table>"; 64: } 65: ?> 66: <html> 67: <head> 68: <title>Posts in Topic</title> 69: </head> 70: <body> 71: <h1>Posts in Topic</h1> 72: <?php echo $display_block; ?> 73: </body> 74: </html> 

Lines 1417 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 19, a message is created in lines 2122, and the script breaks out of the if...else statement and finishes up by printing HTML. This output looks like Figure 20.6.

Figure 20.6. Invalid topic selected.


If, however, the topic is valid, we extract the value of topic_title in line 25, again using stripslashes() to remove any escape characters. Next, a query is issued in lines 2932 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 4044 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 46 we begin to loop through the results of the original query.

The while loop in line 46 says that while there are elements to be extracted from the result set, 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 47. 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 4850. 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 linebreak characters.

In line 53, we continue to write to the $display_block string, using the concatenation operator (.=) to make sure this string is tacked on to the end of the string we have created so far. In line 54, we create the HTML table column to display the author and creation time of the post. The second HTML table row, on line 56, 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 63 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, place it in your Web server document root, and if you have posts in your database tables, you may see something like Figure 20.7.

Figure 20.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 (4th Edition)
ISBN: 067232976X
EAN: 2147483647
Year: 2003
Pages: 333
Authors: Julie Meloni

Similar book on Amazon

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