JavaScript Editor Javascript source editor     Web programming 

Team LiB
Previous Section Next Section

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 20.3, we take a step back and create a page that will list all the topics in the forum. This page will show the basic information of each topic and provide the user with a link to add a new topic; you have already created the form and script for that. The code in Listing 20.3 represents an entry page for your forum.

Although Listing 20.3 looks like a lot of code, it's actually many small, simple concepts you've already encountered, starting with the database connection code in lines 35.

Listing 20.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());
 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>";
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']);
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)');
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:    }
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="addtopic.html">add a topic</a>?</p>
59: </body>
60: </html>

Lines 810 show the first of the database queries, and this particular one selects all the topic information, in order by descending date. In other words, gather the data in such a way that the the topic that was created most recently will appear 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 raw value 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 20.4.

Figure 20.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 assigned to the $display_block variable, 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. 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 assign the value of $topic_info['topic_id'] to the $topic_id variable, meaning that we get a local value for $topic_id from an array called $topic_info, containing a field 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 display string we have built 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 20.5.

Figure 20.5. Topics are available.

    Team LiB
    Previous Section Next Section

    JavaScript Editor Javascript source editor     Web programming