For ...While Loop Start at second Item

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lemonfreshmedia
Forum Commoner
Posts: 26
Joined: Fri Dec 02, 2005 7:14 pm

For ...While Loop Start at second Item

Post by lemonfreshmedia »

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>';


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;
?>
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

start counting from 1 not 0;

Code: Select all

for ($i = 1; $i < $rss_parser->maxitems; $i++)
lemonfreshmedia
Forum Commoner
Posts: 26
Joined: Fri Dec 02, 2005 7:14 pm

usign that i only get the second article

Post by lemonfreshmedia »

I only get the second article using that code. I wonder how I can get all the srticles AFTER the first

Thanks for the effort wtf.
Much appreciated.
Post Reply