Article
Build an XML-Based Content Management System with PHP
Deleting an XML Article
Deleting an XML file is very simple. All you have to do is pass a filename to the delArticle.php page, unlink the file, and send the user back to the adminindex.php page:
<?php
session_start();
if ($_SESSION["login"] != "true"){
header("Location:login.php");
$_SESSION["error"] = "<font color=red>You don't have privileges to see the admin page.</font>";
exit;
}
$dir = "./xml/";
$filetoburn = $dir . $file;
unlink($filetoburn);
header("Location: adminindex.php");
?>
At this point, we've just completed the article management portion of our XML-powered CMS. We've built a login page, an administrative index, and pages for adding, editing, and deleting articles.
In the next part of our article, we'll build the display side of the Website so that visitors can read and search for articles.
Building the Display Side
Now that we've defined the XML article structure and built a very simple, secure administration tool to help us create, edit, delete, and publish files, it's time to build that part of the site that displays articles for site visitors.
Let's recap our display-side requirements:
- Site visitors will be able to view "live" articles listed by headline. They will also be able to perform a search on headline and keywords.
- The site itself will consist of the following pages:
- A home page that lists five articles published on the site and a search function. Furthermore, site visitors can click a link to show all articles.
- An article detail page that displays one article at a time.
- A search result page that will list all articles by an author, keyword, or string entered into the search engine.
The Home Page
The home page, index.php, contains code that basically repeats what we created on the admin side. It basically loops through all the articles in the xml/ directory, opens each file and looks for a status, headline, and abstract. If the status is set to something other than "live," then the loop finds the next article.
The result is a list of article headlines and abstracts displayed to the home page. Each link leads the site visitor to the showArticle.php page.
Here's the code (note that I've included a search widget also):
<?php
function extractText($array){
if(count($array) <= 1){
//we only have one tag to process!
for ($i = 0; $i<count($array); $i++){
$node = $array[$i];
$value = $node->get_content();
}
return $value;
}
}
?>
<html>
<head>
<title>Welcome to XMLTEST</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<h1>Welcome to the XMLTEST site</h1>
<p><br>
<a href="adminindex.php">Admin Login</a> </p>
<form name="search" method="post" action="searchArticles.php">
Search articles:
<input name="search" type="text" id="search">
<input name="Search" type="submit" id="Search" value="Search">
</form>
<p>The following articles are available: </p>
<table border=1 cellspacing=0 cellpadding=2 width=500>
<?php
$dh = opendir('./xml/');
$fileCount = 0;
while ($file = readdir($dh) and $fileCount <= 5){
if (eregi("^\\.\\.?$", $file)) {
continue;
}
$open = "xml/".$file;
$xml = domxml_open_file($open);
//we need to pull out all the things from this file that we will need to
//build our links
$root = $xml->root();
$stat_array = $root->get_elements_by_tagname("status");
$status = extractText($stat_array);
$ab_array = $root->get_elements_by_tagname("abstract");
$abstract = extractText($ab_array);
$h_array = $root->get_elements_by_tagname("headline");
$headline = extractText($h_array);
if ($status != "live"){
continue;
}
echo "<tr valign=top><td>";
echo "<a href=\"showArticle.php?file=".$file . "\">".$headline . "</a><br>";
echo $abstract;
echo "</td></tr>";
$fileCount++;
}
?>
</table>
<br><a href="adminindex.php">Admin Login</a>
</body>
</html>
Show Article Page
The showArticle.php page is where individual articles are displayed.
Everything you learned about pulling text out of an XML structure applies here, too. Basically, you open the XML article based on the filename that you pass to the page. Then you pull out each appropriate node that you need and store them as variables. In the case of our paragraphs, we'll store them in an array so we can print them out with a simple foreach loop. Then we print the variables to the screen.
Here's the code:
<?php
session_start();
function extractText($array){
if(count($array) <= 1){
//we only have one tag to process!
for ($i = 0; $i<count($array); $i++){
$node = $array[$i];
$value = $node->get_content();
}
return $value;
}
}
//pull in the XML file
if ($file == ""){
echo "<h2>You didn't choose a file to edit!</h2>";
echo "<a href=\"index.php\">Go back to index and choose a file</a>";
exit;
} else {
$open = "./xml/" . $file;
$xml = domxml_open_file($open);
$root = $xml->root();
$id = $root->get_attribute("id");
$h_array = $root->get_elements_by_tagname("headline");
$headline = extractText($h_array);
$stat_array = $root->get_elements_by_tagname("status");
$status = extractText($stat_array);
$a_array = $root->get_elements_by_tagname("author");
$author = extractText($a_array);
$e_array = $root->get_elements_by_tagname("email");
$email = extractText($e_array);
$ab_array = $root->get_elements_by_tagname("abstract");
$abstract = extractText($ab_array);
$kl_array = $root->get_elements_by_tagname("keywords");
$keywords = extractText($kl_array);
$lead_array = $root->get_elements_by_tagname("para-intro");
$para["intro"] = extractText($lead_array);
$second_array = $root->get_elements_by_tagname("para-main");
$para["main"] = extractText($second_array);
$con_array = $root->get_elements_by_tagname("para-conclusion");
$para["con"] = extractText($con_array);
?>
<html>
<head>
<title><?php echo $headline; ?></title>
<meta name="keywords" content="<?php echo $keywords; ?>">
<meta name="description" content="<?php echo $abstract; ?>">
</head>
<body>
<h1><?php echo $headline; ?></h1>
<a href="index.php">back to main</a>
<p>by <a href="mailto:<?php echo $email; ?>"><?php echo $author; ?></a></p>
<p><small><?php echo $abstract; ?></small></p>
<?php
foreach ($para as $k => $v){
echo "<p>".$v."</p>\n";
}
?>
</body>
</html>
<?php
}
?>