Site Administration Page 4 - Building a Barebones Content Management System: The Yaapi API |
Review the following code listing (say "category.php") where I’ve integrated yaapi with my HTML template to display a list of articles in a particular category. <?php // the include file // initialize objects // The $id variable should always have a value ?> <HTML> // Get the articles for the selected category if(number_of_elements($articles) == 0) {
echo "<LI><P><A HREF=\"article.php?id=".$articles[$count] } ?> I launch the above PHP script in the browser. Note that I pass an "id" parameter, which represents the unique ID created for each category assigned by yaapi, in the query string: http://localhost/yaapi/category.php?id=1. If all goes well, I should be able to view a list of articles (that I added earlier) as seen in the output, below.
Now, let me quickly take you through the PHP script that has brought about this transformation. <% ..// snip // the include file // snip First, I’ve included the "article.class.php" file. As you have already seen, this PHP file encloses the definition of the all-important article() class. <% // snip // initialize objects // The $id variable should always have a value // snip %> Here, I create an instance of the yaapi article() object and store the category "id" from the query string in a local variable titled "$category_id". I’ll let you in on a little secret, now: in my rush to get started, I created and deleted several categories. As a result, I did not have any category with an "id" value of 1. Here, I could update my PHP scripts to display a polite error message that the category does not exist. While this works fine in a development environment, I do not expect visitors to return if they come across such messages once my website is up and running. This can be easily avoided by the introduction of a default, fall-back category "id" in the configuration file -- a category that I am certain will always contain an article. Take a peek at the updated configuration file: <?php /** // snip /** // snip ?> You’ll notice that I introduced a block titled "yaapi constants." Here, I have defined two new global variables: "DEFAULT_CATEGORY_ID" and "DEFAULT_ARTICLE_ID". The former represent a default category as described above and the latter represents an article that will always exist in the database. Now, let me show you how I’ve used this parameter in my PHP script. There are two scenarios where I would need to use this "default" category. The first one, handled above, is when I do not pass a category id in the query string. The second scenario results when I pass a valid category "id" in the query string; however, there are no articles associated with this category in the database. This can be handled by the PHP code snippet listed below. <% // snip <TD WIDTH="50%" ALIGN="LEFT" VALIGN="TOP"> // Get the articles for the selected category if(number_of_elements($articles) == 0) {
echo "<LI><P><A HREF=\"article.php?id=".$articles[$count] } ?> // snip As seen above, I invoke the get_titles() method of the article() object. This returns an associative array that contains the "article_id" and "title" of the articles belonging to the category (whose id is stored in the "$category_id" variable). I check the number of elements in my $article array with a quick call to the number_of_elements() function. The definition of this custom function can be found in the "inc.config.php" file. <?php //snip /** // Accurately return the number of elements in an array // Initialize count variable to 0 // Check if the element passed is a valid array return $count; // snip ?> Coming back to my "category.php" script: I verify that there is at least one element in the associative array and iterate over the latter to display the title of each article (along with an appropriate hyperlink) on the web page, as seen in the output above. Some of you might point out that the above code snippet can handle both of he scenarios (about an invalid category id in the query string) that I listed earlier. So, why double the effort? The reason is simple: this two-step approach allows me to debug my PHP scripts better. In the future, I may wish to integrate an audit log mechanism and then, I can store appropriate messages in the log file.
blog comments powered by Disqus |
|
|
|
|
|
|
|