sotusotusotu Posted April 11, 2008 Share Posted April 11, 2008 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 More sharing options...
haku Posted April 11, 2008 Share Posted April 11, 2008 Use a for loop. for($i = 0; $i < 15; $i++) Link to comment https://forums.phpfreaks.com/topic/100654-counting-xml/#findComment-514767 Share on other sites More sharing options...
sotusotusotu Posted April 11, 2008 Author Share Posted April 11, 2008 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 More sharing options...
haku Posted April 11, 2008 Share Posted April 11, 2008 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.