For ...While Loop Start at second Item
Posted: Fri Dec 09, 2005 2:10 pm
Can anyone help me with the following problem?
Im parsing some xml and would like to start counting at article number 2(item1 in the array)
How do I modify the script to reflect this?
I beleive it's very simple somehwere here
for ($i = 0; $i < $rss_parser->maxitems; $i++) {
$xml .= $items[$i].'</item>';
Im parsing some xml and would like to start counting at article number 2(item1 in the array)
How do I modify the script to reflect this?
I beleive it's very simple somehwere here
for ($i = 0; $i < $rss_parser->maxitems; $i++) {
$xml .= $items[$i].'</item>';
Code: Select all
<?php
class RSSParser22 {
/* The number of items to retrieve */
var $maxitems = 10;
var $insideitem = false;
var $tag = "";
var $title = "";
var $description = "";
var $link = "";
var $blank = "_blank";
var $none = "nolink";
var $mycounter = "fakdjhfjkadhjklas";
function startElement($parser, $tagName, $attrs) {
if ($this->insideitem) {
$this->tag = $tagName;
} elseif ($tagName == "ITEM") {
$this->insideitem = true;
}
}
function endElement($parser, $tagName) {
if ($tagName == "ITEM") {
printf("<tr><td height=\"42\" class=\"columns-front\"><a href='%s' target=&$blank&>%s</a>",
trim($this->link),htmlspecialchars(trim($this->title)));
printf("<br><b>%s<b></tr></td>",($this->description));
$this->title = "";
$this->description = "";
$this->link = "";
$this->insideitem = false;
}
}
function characterData($parser, $data) {
if ($this->insideitem) {
switch ($this->tag) {
case "TITLE":
$this->title .= $data;
$mycounter++;
break;
case "SOURCE":
$this->description .= $data;
break;
case "LINK":
$this->link .= $data;
break;
}
}
}
}
$xml_parser = xml_parser_create();
$rss_parser = new RSSParser22();
xml_set_object($xml_parser,&$rss_parser);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("http://p.moreover.com/page?o=rss002&c=Gaming%20news","r")
or die("Error reading RSS data.");
/* Reads in all the data at once
into a temporary variable */
while ($data = fread($fp, 4096)) {
$tmp .= $data;
}
/* Splits up (tmp) into items */
$items = split('</item>', $tmp);
/* Cycles thru (items) with (maxitems) iterations */
for ($i = 0; $i < $rss_parser->maxitems; $i++) {
$xml .= $items[$i].'</item>';
}
$xml .= $items[sizeof($items)-1]; //<-- The remaining XML
xml_parse($xml_parser, $xml);
fclose($fp);
xml_parser_free($xml_parser);
$xml = null;
$data =null;
$items =null;
$tmp = null;
?>