Building a Weblog: Part 2 / Page 2 | WebReference

Building a Weblog: Part 2 / Page 2

[previous] [next]

Building a Weblog: Part 2

Displaying Previous Blog Entries

It is often convenient to see the last five or so blog entries, so that if a user misses a few entries, she can access them easily without having to dig through the archives.

First, create the query. Luckily, this query is the same as the one you used to find the latest blog entry—the only difference being that instead of limiting the results to a single entry, you limit the result set to five entries. Do this by changing the LIMIT 1 line to LIMIT 1, 5. This ensures that you get records 0 to 4.


When you use LIMIT, the first record returned is marked as the zeroth. As such, LIMIT 1,5 returns the first record through to the fifth. LIMIT 0, 1 is synonymous with LIMIT 1.

Add the following code to your page:

This query counts the number of rows returned so you can display the relevant information. Now, add the code to display the results:

If no rows were returned in the query, the text No previous entries. is displayed. If rows are returned, the else block is executed and the previous entries are displayed in an unordered list.

Inside the else block, use a while loop to iterate through the results from the query to create the blog entry subjects with the <li> and </li> tags. The subject is linked to viewentry.php with the relevant id appended as a variable in the link. The end result is shown in Figure 4-5.

Figure 4-5


Ordered and Unordered Lists

Within HTML, Web developers often use ordered and unordered lists to create bullet points. To create a numbered list, you use the <ol> and </ol> ordered list tags. To create an unnumbered bullet point list, use the unordered <ul> and </ul> tags.

List items are placed inside <li> and </li> tags. An example of an unordered list is shown as follows:

[previous] [next]