Displaying the Topic List
Now that you have a topic and at least one post in your database, you can display this information and let people add new topics or reply to existing ones. In Listing 19.3, we take a step back and create a topic listing page. This page will show the basic information of each topic and provide the user with a link to add a new topic; you already have the form and script for that. This script would actually be an entry page for your forum.
Listing 19.3 Topic Listing Script
1: <?php
2: //connect to server and select database
3: $conn = mysql_connect("localhost", "joeuser", "somepass")
4: or die(mysql_error());
5: mysql_select_db("testDB",$conn) or die(mysql_error());
6:
7: //gather the topics
8: $get_topics = "select topic_id, topic_title,
9: date_format(topic_create_time, '%b %e %Y at %r') as fmt_topic_create_time,
10: topic_owner from forum_topics order by topic_create_time desc";
11: $get_topics_res = mysql_query($get_topics,$conn) or die(mysql_error());
12: if (mysql_num_rows($get_topics_res) < 1) {
13: //there are no topics, so say so
14: $display_block = "<P><em>No topics exist.</em></p>";
15: } else {
16: //create the display string
17: $display_block = "
18: <table cellpadding=3 cellspacing=1 border=1>
19: <tr>
20: <th>TOPIC TITLE</th>
21: <th># of POSTS</th>
22: </tr>";
23:
24: while ($topic_info = mysql_fetch_array($get_topics_res)) {
25: $topic_id = $topic_info['topic_id'];
26: $topic_title = stripslashes($topic_info['topic_title']);
27: $topic_create_time = $topic_info['fmt_topic_create_time'];
28: $topic_owner = stripslashes($topic_info['topic_owner']);
29:
30: //get number of posts
31: $get_num_posts = "select count(post_id) from forum_posts
32: where topic_id = $topic_id";
33: $get_num_posts_res = mysql_query($get_num_posts,$conn)
34: or die(mysql_error());
35: $num_posts = mysql_result($get_num_posts_res,0,'count(post_id)');
36:
37: //add to display
38: $display_block .= "
39: <tr>
40: <td><a href="showtopic.php?topic_id=$topic_id">
41: <strong>$topic_title</strong></a><br>
42: Created on $topic_create_time by $topic_owner</td>
43: <td align=center>$num_posts</td>
44: </tr>";
45: }
46:
47: //close up the table
48: $display_block .= "</table>";
49: }
50: ?>
51: <html>
52: <head>
53: <title>Topics in My Forum</title>
54: </head>
55: <body>
56: <h1>Topics in My Forum</h1>
57: <?php echo $display_block; ?>
58: <P>Would you like to <a href=">add a topic</a>?</p>
59: </body>
60: </html>
Although Listing 19.3 looks like a lot of code, it's actually many small, simple concepts you've already encountered. Lines 35 make the connection to the database, in preparation for issuing queries. Lines 810 show the first of these queries, and this particular one selects all the topic information, in order by descending date. In other words, display the topic that was created last (the newest topic) at the top of the list. In the query, notice the use of the date_format() function to create a much nicer date display than the one stored in the database.Line 12 checks for the presence of any records returned by the query. If no records are returned, and therefore no topics are in the table, you'll want to tell the user. Line 14 creates this message. At this point, if no topics existed, the script would break out of the if...else construct and be over with; the next action would occur at line 51, which is the start of the static HTML. If the script ended here, the message created in line 14 would be printed in line 57, and you would see something like Figure 19.4.
Figure 19.4. No topics found.

If, however, you have topics in your forum_topics table, the script continues at line 15. At line 17, a block of text is started, containing the beginnings of an HTML table. Lines 1822 set up a table with two columns: one for the title and one for the number of posts. The text block is stopped momentarily, and at line 24 we begin to loop through the results of the original query.The while loop in line 24 says that while there are elements to be extracted from the result set, extract each row as an array called $topic_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 topic_id field, on line 25. We set the value of $topic_id to $topic_info['topic_id'], meaning that we get a local value for $topic_id from an array called $topic_info, containing a slot called topic_id. Continue doing this for the $topic_title, $topic_create_time, and $topic_owner variables in lines 2628. The stripslashes() function removes any escape characters that were input into the table at the time of record insertion.In lines 3135 we issue another query, in the context of the while loop, to get the number of posts for that particular topic. In line 38, we continue the creation of 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 40, we create the HTML table column to display the link to the file that will show the topic (showtopic.php), and also print the topic owner and creation time. The second HTML table column, on line 43, shows the number of posts. On line 45, we break out of the while loop, and in line 48 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 topiclist.php and place it in your Web server document root, and if you have topics in your database tables, you may see something like Figure 19.5.
Figure 19.5. Topics are available.
