I have set up an XML parser with a tutorial i got here on kirupa it consists of two files, ( index.php) and (xml.xml) - I simply used there XML parser tutorial and added a third string. HINT HINT** <NEWS> <STORY>
xml.xml
<?xml version="1.0"?>
<news>
<story>
<title>title</title>
<image>imageURL</image>
<css>css code</css>
<title>title</title>
<image>imageURL</image>
<css>css code</css>
<title>title</title>
<image>imageURL</image>
<css>css code</css>
<title>title</title>
<image>imageURL</image>
<css>css code</css>
<title>title</title>
<image>imageURL</image>
<css>css code</css>
<title>title</title>
<image>imageURL</image>
<css>css code</css>
</story>
</news>
index.php
<?php
$xml_file = "xml.xml";
$xml_title_key = "*NEWS*STORY*TITLE";
$xml_image_key = "*NEWS*STORY*IMAGE";
$xml_css_key = "*NEWS*STORY*CSS";
$story_array = array();
$counter = 0;
class xml_story{
var $title, $image, $css;
}
function startTag($parser, $data){
global $current_tag;
$current_tag .= "*$data";
}
function endTag($parser, $data){
global $current_tag;
$tag_key = strrpos($current_tag, '*');
$current_tag = substr($current_tag, 0, $tag_key);
}
function contents($parser, $data){
global $current_tag, $xml_title_key, $xml_image_key, $xml_css_key, $counter, $story_array;
switch($current_tag){
case $xml_title_key:
$story_array[$counter] = new xml_story();
$story_array[$counter]->title = $data;
break;
case $xml_image_key:
$story_array[$counter]->image = $data;
break;
case $xml_css_key:
$story_array[$counter]->css = $data;
$counter++;
break;
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
$fp = fopen($xml_file, "r") or die("Could not open file");
$data = fread($fp, filesize($xml_file)) or die("Could not read file");
if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}
xml_parser_free($xml_parser);
fclose($fp);
?>
<html>
<head>
<title>CNT HEADLINE NEWS</title>
</head>
<body bgcolor="#FFFFFF">
<?php
for($x=0;$x<count($story_array);$x++){
echo " <h2>" . $story_array[$x]->title . "</h2>
";
echo "
";
echo " <i>" . $story_array[$x]->css . "" . $story_array[$x]->image . "</i>
";
}
?>
</body>
</html>
Now the above index and xml work together really smooth, but then i try’d to paginate the results I got back. And since all I could find was how to do this with SQL it ended up not working… Now the code below is for my attempt at paginating the results. Any idea how to fix it?
paginated index.php file
<?php
$xml_file = "xml.xml";
$xml_title_key = "*NEWS*STORY*TITLE";
$xml_image_key = "*NEWS*STORY*IMAGE";
$xml_css_key = "*NEWS*STORY*CSS";
$story_array = array();
$counter = 0;
class xml_story{
var $title, $image, $css;
}
function startTag($parser, $data){
global $current_tag;
$current_tag .= "*$data";
}
function endTag($parser, $data){
global $current_tag;
$tag_key = strrpos($current_tag, '*');
$current_tag = substr($current_tag, 0, $tag_key);
}
function contents($parser, $data){
global $current_tag, $xml_title_key, $xml_image_key, $xml_css_key, $counter, $story_array;
switch($current_tag){
case $xml_title_key:
$story_array[$counter] = new xml_story();
$story_array[$counter]->title = $data;
break;
case $xml_image_key:
$story_array[$counter]->image = $data;
break;
case $xml_css_key:
$story_array[$counter]->css = $data;
$counter++;
break;
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
$fp = fopen($xml_file, "r") or die("Could not open file");
$data = fread($fp, filesize($xml_file)) or die("Could not read file");
if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}
xml_parser_free($xml_parser);
fclose($fp);
?>
<html>
<head>
<title>CNT HEADLINE NEWS</title>
</head>
<body bgcolor="#FFFFFF">
<?php
// set the amnt of results per pgae
$perpage=5;
// get current page num
$page = (int)$_GET['page'];
$page = $page ? $page : 0;
// Decides where to start and end
$c = count($story_array);
$start = 0 > $start ? 0 : $start;
$end = $c > $end ? $end : $c;
for($x=$start;$x<$end;$x++){
echo " <h2>" . $story_array[$x]->title . "</h2>
";
// echo "
";
echo " <i>" . $story_array[$x]->css . "" . $story_array[$x]->image . "</i>
";
}
echo '<br /><br />Viewing results '.(int)$start.' through '.(int)$end.' of '.(int)$c;
for ($i=0;$i<=$end/$perpage;$i++) {
echo '<a href="index.php?page='.$i.'">'.$i.'</a> ';
}
?>
</body>
</html>