Jump to content

Counting XML


sotusotusotu

Recommended Posts

Hi guys,

 

I haven't really worked with PHP and XML before but I need to only display 15 records of my code. Any ideas?

 

function readXML(){

$doc = new DOMDocument();

$doc->load( 'hbnews.xml' );

$newsFeeds = $doc->getElementsByTagName( "story" );

foreach( $newsFeeds as $newsFeed ){

 

$titles = $newsFeed->getElementsByTagName( "title" );

$title = $titles->item(0)->nodeValue;

 

$bodys = $newsFeed->getElementsByTagName( "body" );

$body = $bodys->item(0)->nodeValue;

 

$dates = $newsFeed->getElementsByTagName( "date" );

$date = $dates->item(0)->nodeValue;

 

echo "<p><b>$title</b></p>

  <p>$body</p>

  <p>$date</p><br />";

}

}

readXML();

Link to comment
https://forums.phpfreaks.com/topic/100654-counting-xml/
Share on other sites

Cheers Haku for the reply.

 

I am sure I am being stupid here, but it is not limiting to 15.

 

function readXML(){

$doc = new DOMDocument();

$doc->load( 'hbnews.xml' );

$newsFeeds = $doc->getElementsByTagName( "story" );

for($newsFeeds = 0; $newsFeeds < 15; $newsFeeds++){

 

$titles = $newsFeed->getElementsByTagName( "title" );

$title = $titles->item(0)->nodeValue;

 

$bodys = $newsFeed->getElementsByTagName( "body" );

$body = $bodys->item(0)->nodeValue;

 

$dates = $newsFeed->getElementsByTagName( "date" );

$date = $dates->item(0)->nodeValue;

 

echo "<p><b>$title</b></p>

  <p>$body</p>

  <p>$date</p><br />";

}

}

readXML();

 

Link to comment
https://forums.phpfreaks.com/topic/100654-counting-xml/#findComment-514882
Share on other sites

I'll try and clean up your code, but not knowing what your goal is, I could be off a little. But I can probably solve the immediate problem you have having with your for loop:

 

function readXML(){
   $doc = new DOMDocument();
   $doc->load( 'hbnews.xml' );
      $newsFeeds = $doc->getElementsByTagName( "story" );
      for($i = 0; $i < 15; $i++) {

         $titles = $newsFeeds[$i]->getElementsByTagName( "title" );
         $title = $titles->item(0)->nodeValue;
         
         $bodys = $newsFeeds[$i]->getElementsByTagName( "body" );
         $body = $bodys->item(0)->nodeValue;
          
         $dates = $newsFeeds[$i]->getElementsByTagName( "date" );
         $date = $dates->item(0)->nodeValue;
       
         echo "<p>$title</p>
              <p>$body</p>
              <p>$date</p>";
      }
}
readXML();

 

I only cleaned up your for statement syntax. The rest is up to you.

Link to comment
https://forums.phpfreaks.com/topic/100654-counting-xml/#findComment-515136
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.