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

yaapi -- Display An Article - 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

After having shown you how to list the articles in a category, I will now demonstrate how to display the contents of an article in the list. You may have noticed that each article title in the previous example was linked to a script titled "article.php" (with the "id" of the article passed in the query string). It's time to review the PHP script.

<?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"] == "") {
 $article_id = $GLOBALS['DEFAULT_ARTICLE_ID']; // Set this to a default article ID
} else {
 $article_id = $_GET["id"];
}

// The $id variable should always have a value
if(!isset($_GET["current_page"]) || $_GET["current_page"] == "") {
 $current_page = 1; // Set current page value to 1, if not set
}

?>

<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 intro for the article
   $current_article = $article->get_article($article_id);

   if(!isset($current_article) || !$current_article) {
    $current_article = $article->get_article($GLOBALS['DEFAULT_ARTICLE_ID']);
   }

   echo "<H2>$current_article->title</H2>";
   echo "<H5>Author: <A HREF=\"
mailto:$current_article->email\">$current_article->author</A>";
   echo "<BR CLEAR=\"all\">Date Published: $current_article->date</H5>";
   echo "<P>$current_article->content</P>";

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

Click on the title of any article listed by "category.php" script to view the following output:

Now, let me guide you through the contents of my second PHP script. 

<?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"] == "") {
 $article_id = $GLOBALS['DEFAULT_ARTICLE_ID']; // Set this to a default article ID
} else {
 $article_id = $_GET["id"];
}

// snip
?>

Youíll notice that the first few lines are similar to those in "category.php": after including "article.class.php", I proceed to create an instance of the article() class for use in this script. Next, I assign value to the "$article_id" local variables. If I do not find a value in the query string, I assign the default value stored in the configuration file, as explained in the previous section.

<%

// snip

<?php

// Get the intro for the article
$current_article = $article->get_article($article_id);

if(!isset($current_article) || !$current_article) {
  $current_article = $article->get_article($GLOBALS['DEFAULT_ARTICLE_ID']);
}

echo "<H2>$current_article->title</H2>";
echo "<H5>Author: <A HREF=\"
mailto:$current_article->email\">$current_article->author</A>";
echo "<BR CLEAR=\"all\">Date Published: $current_article->date</H5>";
echo "<P>$current_article->content</P>";

?>

// snip

%>

Now that I have an "id" for the article, I proceed to invoke get_article(). This aptly titled method returns the data associated with the article whose "id" is passed as the input to the method. I store the return value in another local variable, "$current_article" and use appropriate properties of the article() object to render the screen, as seen in the output.

For your reference, I have listed the properties of the article() object below:

  • title: the title of the article; youíve already seen this property in action in the previous section.
  • author: the author (or authoress).
  • email: the email address entered in the database.
  • date: the data and time when the entry was inserted in the database.
  • content: the actual text for the article.

With this example behind me, I can safely conclude that Iíve now demonstrated two critical features that any practical CMS should provide: the ability to list the articles in the database, and then retrieve the actual content that constitutes an article.



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