Article
Build an XML-Based Content Management System with PHP
Searching Through Articles
Searching through an XML article archive is a lot like searching through a database table, except that with XML, you need to open each file and compare the search term with each of the nodes you want to search on, collect pertinent data (like filename, headline, and abstract) and then display these on a page.
In the case of our example, I'm going to use the quick and easy route—opening all files in order, checking to see if they are live (if they aren't live, I go on to the next file), and storing the headline, abstract, and filename in a multidimensional array.
When I send the results to the screen, I make sure that I count the number of records in this array. If the count is 0, then I display a message saying that no files matched the search term. Otherwise, I display a linked headline and abstract for each article I find.
This search algorithm is the result of three minutes of thinking and about ten minutes of implementing. It is fast and dirty, and works like a charm if you have a small number of articles. In future articles, we'll look at more robust methods for searching XML files.
Here's the code:
<?php
session_start();
$results = array();
//this is a very simple, potentially very slow search
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;
}
}
$dh = opendir('./xml/');
while ($file = readdir($dh)){
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);
$k_array = $root->get_elements_by_tagname("keywords");
$keywords = extractText($k_array);
$h_array = $root->get_elements_by_tagname("headline");
$headline = extractText($h_array);
$ab_array = $root->get_elements_by_tagname("abstract");
$abstract = extractText($ab_array);
if ($status != "live"){
continue;
}
if (eregi($searchTerm, $keywords) or eregi($searchTerm,$headline)){
$list['abstract'] = $abstract;
$list['headline'] = $headline;
$list['file'] = $file;
$results[] = $list;
}
}
$results = array_unique($results);
?>
<h1>Search Results</h1>
<a href="index.php">back to main</a>
<p>You searched for: <i><?php echo $searchTerm ?></i></p>
<hr>
<?php
if (count($results)>0){
echo "<p>Your search results:</p>";
foreach ($results as $key => $listing){
echo "<br><a href=\"showArticle.php?file=".$listing["file"]."\">" . $listing["headline"]."</a>\n";
echo "<br>". $listing["abstract"];
echo "<br>";
}
} else {
echo "<p>Sorry, no articles matched your search term.";
}
?>
Conclusion
That's it! Well done -- we've covered a lot of territory. We learned:
- Some rudimentary tools for developing XML structures
- How to build a very simple and secure admin tool to take care of basic site functions
- How to build a home page, article view page, and a simple search engine
In each case, there's room for improvement, refinement, and robustness. The key is, while working with the tools, you were exposed to some of the basic concepts of XML data structures, application workflow, and simple CMS design.