Building a Weblog: Part 3
[next]
Building a Weblog: Part 3
Building the Category Browser
Within a site powered by Blogtastic, a large number of blog entries is going to build. With so much content available, it is important to have a means of easily browsing this content. In this section, you create a useful page for users to browse the different categories and see which blog entries have been posted in each category.
If you think about how this page should be designed, it seems logical to list the categories and let the user click on one to see any related blog entries (see Figure 4-7). This functionality is similar to a tree view in a file manager: The directories are listed, and then you click one to see the files and subdirectories.
On index.php and viewentry.php, you made the category a link to a page called viewcat.php, and the ID of the category was passed as an
id GET
variable. To get started, create a new file called viewcat.php and add the following code:
This code should look familiar; it runs the id variable through the same validation tests used on viewentry.php. If no variable exists, validcat
is set to 0, but if the variable is indeed legitimate, validcat
is set to the contents of the GET
variable. If the variable fails the test to check if it is numeric, the page redirects to itself but without the id
variable.
Select all of the records from the categories table:
Add the following code to check each row of the result set and see if $validcat is the same as the idvariable. If it is, this means that the category is currently selected.
As the while loop iterates through each row, the first line checks if validcat is the same as the ID from the current row. If it is, the if block is executed. The first line inside the if outputs the name of the category in bold, instead of a link.
The query on the next line gets all blog entries in which cat_id is equal to validcat. These entries are requested in descending date order, so the most recent entry will display at the top of the list. The query is then run, and the returned rows are counted (to ensure that there are records to show). The final line starts the unordered list block that contains the results.
Check to see if any rows exist for the current category and display the relevant details:
If numrows_entries has zero rows, the browser displays a list item with the text No entries!
. If there are rows, another while loop is opened to run through the results. Inside this while
, a list item that displays the date of the entry and a link to viewentry.php (using the correct id
value) is created. The subject of the post is the body of the link.
Finally, you can display the currently unselected categories:
You now have a complete archive of blog entries organized by category!
[next]
URL: