Building a Barebones Content Management System: The Yaapi API - yaapi -- Display An Article (Page 5 of 6 )
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> </P>
<H1>BB_CMS - A Barebones Content Management System</H1>
<P> </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> </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>© 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.
Next: yaapi - Display List Of Categories >>
More Administration Articles
More By Harish Kamath