Building a Weblog: Part 2 | WebReference

Building a Weblog: Part 2


Building a Weblog: Part 2

By Jono Bacon

Adding a Comments Summary

One of the planned features for this blog is the capability for visitors to add comments to a blog entry. These comments should be visible on the viewentry.php page, linked via the subject of the blog (which you just added to index.php).

When comments are posted to a blog entry, it's helpful to provide a comments summary. When comments have been posted, you can display the number of comments and the names of the posters. It's also useful to have the names of the posters double as hyperlinks; that is, when you click the poster's name, the application jumps to that poster's comment on the viewentry.php page.

After the code already in place in index.php, add the following lines:

This chunk of code creates a new paragraph tag, and then a new SQL query to select the name field from the comments table, where blog_id contains the id of the current blog entry (stored in $row['id']). The entire query is ordered by date (using the dateposted field). This query is then executed with the mysql_query() command, and the result is stored in $commresult.

On the final line, a new function called mysql_num_rows() is used to count how many rows are returned from the query, and this number is stored in the $numrows_comm variable. The mysql_num_rows() function is incredibly useful, because you can use it to determine how to format the comments summary. If no rows are returned, display 'No comments'; if 1 or more results are returned, display the posters' names:

In this block of code, an if statement is used to check if $numrows_comm has 0 rows. If it does, No comments is echoed to the screen. If $numrows_comm is not equal to 0, control moves into the else statement.

Inside the else, an echo line prints a bracket and then, in bold typeface, outputs the number of rows stored in $numrows_comm and finally outputs a closing bracket and the word comments. If there were two comments, the output would be

(2) comments

The next step is to display each comment, as well as a link to that comment, using an anchor.

The anchors used in viewentry.php are in the form #comment1, #comment2, and so on. To add these numbered anchors in index.php, start at 1 and increment each time a comment link is output.


Anchors are handy methods of linking to different parts of a single page. To reference an anchor, you add the name of the anchor to the URL. As an example, linking to example.php#theory jumps to the theory anchor on the example.php page. At some point in example.php, there should be something like this:

<a name="theory">

Now, when example.php#theory is referenced, the page will jump to that tag.

Back in the code, you'll see that a variable called $i is created and set to 1. Next, a while loop iterates through the rows. A link to viewentry.php is created, and id=[] is added to each. In addition to the id being appended, the comment anchor (such as #comment1) is added, using $i. Finally, the value of $i is increased by 1, ready for use on the next link. The completed output should look something like this (obviously with different names if you have added different comments):
(2) comments : Jim Bob

If you are using the sample detail discussed earlier in the chapter, you will continue to see "No comments" because no comments are associated with the second blog entry. To resolve this, use phpMyAdmin to add some records to the comments table and specify a value of 2 in the blog_id field.

You can see the comments shown in Figure 4-4.

Figure 4-4