Home arrow Site Administration arrow Page 4 - Building a Barebones Content Management System: The Yaapi API

yaapi -- Listing Articles - Administration

This article will show you how to use the different API methods of yaapi, which is an API tool useful for managing content. It will explain how to retrieve a list of articles, display an article on a web page, retrieve a list of categories, and more.

TABLE OF CONTENTS:
  1. Building a Barebones Content Management System: The Yaapi API
  2. Under The Hood
  3. yaapi -- Getting Started
  4. yaapi -- Listing Articles
  5. yaapi -- Display An Article
  6. yaapi - Display List Of Categories
By: Harish Kamath
Rating: starstarstarstarstar / 6
September 21, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
include_once("./article.class.php");

// initialize objects
$article = new article;

// The $id variable should always have a value
if(!isset($_GET["id"]) || $_GET["id"] == "") {
 $category_id = $GLOBALS['DEFAULT_CATEGORY_ID']; // Set this to a default
category ID
} else {
 $category_id = $_GET["id"];
}

?>

<HTML>
<HEAD>
<BASEFONT FACE="Arial">
</HEAD>
<BODY>
<TABLE WIDTH="100%" CELLSPACING="0" CELLPADDING="5" ALIGN="CENTER"
VALIGN="TOP" HEIGHT="450" BORDER="1">
<TR>
 <TD COLSPAN="2" WIDTH="100%" ALIGN="CENTER">
  <P>&nbsp;</P>
  <H1>BB_CMS - A Barebones Content Management System</H1>
  <P>&nbsp;</P>
 </TD>
</TR>
<TR HEIGHT="350">
 <TD WIDTH="25%" ALIGN="MIDDLE" VALIGN="TOP">
  <P><A HREF="#">LINK 1</A></P>
  <P><A HREF="#">LINK 2</A></P>
  <P><A HREF="#">LINK 3</A></P>
  <P><A HREF="#">.. and so on.</A></P>
 </TD>
 <TD WIDTH="50%" ALIGN="LEFT" VALIGN="TOP">
  <P>&nbsp;</P>
  <UL>
  <?php

   // Get the articles for the selected category
   $articles = $article->get_titles($category_id);

   if(number_of_elements($articles) == 0) {
    $articles = $article->get_titles($GLOBALS['DEFAULT_CATEGORY_ID']);
   }


   // now, we are certain that the category will always have at least one
article
   for ($count = 0; $count < number_of_elements ($articles); $count++) {

     echo "<LI><P><A HREF=\"article.php?id=".$articles[$count]
['id']."\">".$articles[$count]['title']."</A></P></LI>";

   }

  ?>
  </UL>
 </TD>
</TR>
<TR>
 <TD COLSPAN="2" WIDTH="100%" ALIGN="CENTER">
  <H5><A HREF="#">Copyright</A> | <A HREF="#">Privacy Policy</A></H5>
  <BR CLEAR="all" />
  <H6>&copy; 2005 BB_CMS Inc.</H6>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>

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
include_once("./article.class.php");

// 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
$article = new article;

// The $id variable should always have a value
if(!isset($_GET["id"]) || $_GET["id"] == "") {
 $category_id = $GLOBALS['DEFAULT_CATEGORY_ID']; // Set this to a default
category ID
} else {
 $category_id = $_GET["id"];
}

// 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

/**
* Configuration file
*
*/

// snip

/**
 * yaapi constants
 */
$DEFAULT_CATEGORY_ID = 3;
$DEFAULT_ARTICLE_ID = 1;

// 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">
  <?php

   // Get the articles for the selected category
   $articles = $article->get_titles($category_id);

   if(number_of_elements($articles) == 0) {
    $articles = $article->get_titles($GLOBALS['DEFAULT_CATEGORY_ID']);
   }


   // now, we are certain that the category will always have at least one
article
   for ($count = 0; $count < number_of_elements ($articles); $count++) {

     echo "<LI><P><A HREF=\"article.php?id=".$articles[$count]
['id']."\">".$articles[$count]['title']."</A></P></LI>";

   }

  ?>
 </TD>

 // 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

/**
 * Utility function definitions
 */

// Accurately return the number of elements in an array
function number_of_elements($array) {

 // Initialize count variable to 0
 $count = 0;

 // Check if the element passed is a valid array
 if(is_array($array)) {
  while (list($key, $value) = each($array)) {
   if ($value) {
    $count++;
   }
  }
 }

 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.



 
 
>>> More Site Administration Articles          >>> More By Harish Kamath
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

SITE ADMINISTRATION ARTICLES

- Coding: Not Just for Developers
- To Support or Not Support IE?
- Administration: Networking OSX and Win 7
- DotNetNuke Gets Social
- Integrating MailChimp with Joomla: Creating ...
- Integrating MailChimp with Joomla: List Mana...
- Integrating MailChimp with Joomla: Building ...
- Integrating MailChimp with Joomla
- More Top WordPress Plugins for Social Media
- Optimizing Security: SSH Public Key Authenti...
- Patches and Rejects in Software Configuratio...
- Configuring a CVS Server
- Managing Code and Teams for Cross-Platform S...
- Software Configuration Management
- Back Up a Joomla Site with Akeeba Backup

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: