Hi all
I have this XML file containing news articles, and I wish to use PHP to parse and then display requested articles.
PROBLEM
The problem is the articles do not display correctly, in particular:
-$articleContent contains data from BOTH articles
-when using showRecentArticles(), Article ID 1 shows up twice, the first time containing both article content from Articles ID 1 and 2, and the second time containing article content from Articles 1,2, then 1 and 2 again.
I hope I can get help fixing this code, and also welcome all suggestions pertaining to rewriting code to improve performance.
INSTRUCTIONS
newsHandler.php (the PHP code)
<?php
//CONFIGURATION SECTION
$xmlSource = "news.xml"; //Location of your news XML file
$noRecentArticles = 2; //Number of recent articles to be shown
$articleFormat = '
<table class="newsholder">
<tr>
<td style="width:100px" align="center" colspan="1" rowspan="3">
<div class="bold">[AUTHORNAME]</div>
<div>[AUTHORRANK]</div>
<img src="[AUTHORPICT]" alt="" />
</td>
<td style="width:450px;" colspan="1" rowspan="1">
<div class="newstitle">[TITLE]</div>
<ins><hr class="newsDivider" /></ins>
</td>
</tr>
<tr>
<td style="width:450px;" colspan="1" rowspan="1">
<div class="newscontent">
[CONTENT]
</div>
</td>
</tr>
<tr>
<td style="width:450px;">
<div class="date">[DATE]</div>
</td>
</tr>
</table>
<ins><img src="img/area.jpg" width="550" height="25" alt="" /></ins>
';
//GLOBAL CONSTANTS SECTION
define("OPTION_UNDEFINED", 0);
define("OPTION_GET_ARTICLE", 1);
define("OPTION_GET_LATEST_ARTICLE_ID", 2);
//GLOBAL VARIABLES SECTION
$requestedArticle = 0;
$foundArticle = 0;
$resultString = "";
$currentNode = "";
$handlerMode = 0;
$articleId = 0;
$articleTitle = "";
$articleDate = "";
$articleAuthorName = "";
$articleAuthorRank = "";
$articleAuthorPict = "";
$articleContent = "";
//FRONT-END SECTION
function showArticle($articleId) {
getArticle($articleId);
transformArticle();
print $GLOBALS["resultString"];
}
function showRecentArticles() {
$temp = getLatestArticleId();
$GLOBALS["resultString"] = "";
for ($i = $temp; $i > ($temp - $GLOBALS["noRecentArticles"]); $i--) {
//Get and display that specific article
getArticle($i);
transformArticle();
print $GLOBALS["resultString"];
}
}
//BACK-END SECTION
function getArticle($articleId) {
//Set global variables
$GLOBALS["requestedArticle"] = $articleId;
$GLOBALS["handlerMode"] = OPTION_GET_ARTICLE;
//Parse XML
$file = fopen($GLOBALS["xmlSource"], "r") or die ("Unable to open XML file");
$xmlParser = xml_parser_create();
xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, false);
xml_parser_set_option($xmlParser, XML_OPTION_SKIP_WHITE, true);
xml_set_element_handler($xmlParser, "startElement", "endElement");
xml_set_character_data_handler($xmlParser, "cData");
while ($s = fread($file, 4096)) {
xml_parse($xmlParser, $s, feof($file));
}
xml_parser_free($xmlParser);
//Reset global variables
$GLOBALS["requestedArticle"] = 0;
$GLOBALS["handlerMode"] = OPTION_UNDEFINED;
}
function getLatestArticleId() {
//Set global variables
$GLOBALS["requestedArticle"] = 0;
$GLOBALS["handlerMode"] = OPTION_GET_LATEST_ARTICLE_ID;
//Parse XML
$file = fopen($GLOBALS["xmlSource"], "r") or die ("Unable to open XML file");
$xmlParser = xml_parser_create();
xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, false);
xml_parser_set_option($xmlParser, XML_OPTION_SKIP_WHITE, true);
xml_set_element_handler($xmlParser, "startElement", "endElement");
xml_set_character_data_handler($xmlParser, "cData");
while ($s = fread($file, 4096)) {
xml_parse($xmlParser, $s, feof($file));
}
xml_parser_free($xmlParser);
$result = $GLOBALS["requestedArticle"];
//Reset global variables
$GLOBALS["requestedArticle"] = 0;
$GLOBALS["handlerMode"] = OPTION_UNDEFINED;
//Return result
return $result;
}
function transformArticle() {
$result = $GLOBALS["articleFormat"];
//Replace entities with values
$result = str_replace('[ID]', $GLOBALS["articleId"], $result);
$result = str_replace('[TITLE]', $GLOBALS["articleTitle"], $result);
$result = str_replace('[DATE]', $GLOBALS["articleDate"], $result);
$result = str_replace('[AUTHORNAME]', $GLOBALS["articleAuthorName"], $result);
$result = str_replace('[AUTHORRANK]', $GLOBALS["articleAuthorRank"], $result);
$result = str_replace('[AUTHORPICT]', $GLOBALS["articleAuthorPict"], $result);
$result = str_replace('[CONTENT]', $GLOBALS["articleContent"], $result);
$GLOBALS["resultString"] = $result;
}
function startElement($parser, $name, $attribs) {
$GLOBALS["currentNode"] = $name;
if ($GLOBALS["handlerMode"] == OPTION_GET_ARTICLE) {
if ($GLOBALS["currentNode"] == "article") {
$GLOBALS["articleId"] = $attribs["id"];
if (strcmp($GLOBALS["articleId"], $GLOBALS["requestedArticle"]) == 0) {
$GLOBALS["foundArticle"] == 1;
}
}
else if ($GLOBALS["currentsNode"] == "a") {
$GLOBALS["articleContent"] .= "<a class=\"newsLink\" href=\"" . $attribs["href"] . "\">";
}
elseif ($GLOBALS["currentNode"] == "link") {
$GLOBALS["articleContent"] .= "<ins id=\"" . $attribs["id"] . "\" style=\"" . $attribs["style"] . "\" onmouseover=\"" . $attribs["onmouseover"] . "\" onmouseout=\"" . $attribs["onmouseout"] . "\" onclick=\"" . $attribs["onclick"] . "\">";
}
elseif ($GLOBALS["currentNode"] == "br") {
$GLOBALS["articleContent"] .= "<ins><br /></ins>";
}
elseif ($GLOBALS["currentNode"] == "img") {
$GLOBALS["articleContent"] .= "<img src=\"" . $attribs['src'] . "\" alt=\"" . $attribs['alt'] . "\" style=\"" . $attribs['style'] . "\" />";
}
}
elseif ($GLOBALS["handlerMode"] == OPTION_GET_LATEST_ARTICLE_ID) {
if ($GLOBALS["currentNode"] == "article") {
$GLOBALS["articleId"] = (int) $attribs["id"];
if ($GLOBALS["articleId"] > $GLOBALS["requestedArticle"]) {
$GLOBALS["requestedArticle"] = $GLOBALS["articleId"];
}
}
}
else {
die ("Undefined option for function startElement()");
}
}
function endElement($parser, $name) {
if ($GLOBALS["currentNode"] == "article") {
//Reset article information
$GLOBALS["articleTitle"] = "";
$GLOBALS["articleDate"] = "";
$GLOBALS["articleAuthorName"] = "";
$GLOBALS["articleAuthorRank"] = "";
$GLOBALS["articleAuthorPict"] = "";
$GLOBALS["articleContent"] = "";
}
if ($GLOBALS["currentNode"] == "a") {
$GLOBALS["articleContent"] .= "</a>";
$GLOBALS["currentNode"] = "content";
}
elseif ($GLOBALS["currentNode"] == "br") {
$GLOBALS["currentNode"] = "content";
}
elseif ($GLOBALS["currentNode"] == "link") {
$GLOBALS["articleContent"] .= "</ins>";
$GLOBALS["currentNode"] = "content";
}
else {
//Reset global variables
$GLOBALS["foundArticle"] = 0;
$GLOBALS["currentNode"] = "";
}
}
function cData($parser, $data) {
if ($GLOBALS["handlerMode"] == OPTION_GET_ARTICLE) {
//Set variable values
switch ($GLOBALS["currentNode"]) {
case "article":
break;
case "title":
$GLOBALS["articleTitle"] = $data;
break;
case "date":
$GLOBALS["articleDate"] = $data;
break;
case "name":
$GLOBALS["articleAuthorName"] = $data;
break;
case "rank":
$GLOBALS["articleAuthorRank"] = $data;
break;
case "pict":
$GLOBALS["articleAuthorPict"] = $data;
break;
case "content":
$GLOBALS["articleContent"] .= $data;
break;
case "link":
$GLOBALS["articleContent"] .= $data;
break;
}
}
}
?>
To be continued...