November 12, 200817 yr This script is meant to pull 4 <content> tags from an external page and echo the values. However, it seems to echo all of the <content> tag values but I don't understand why my counter loop is not working. <?php $reader = new XMLReader; $res = $reader->open('http://www.blogger.com/feeds/17146895/posts/default'); $entry = false; for ($i=1; $i<=4; $i++){ while ($reader->read()) { if ($reader->name == 'entry') { if ($reader->nodeType == XMLReader::ELEMENT) { $entry = true; } else { $entry = false; } } if ($entry && ($reader->name == 'content')) { $reader->read(); if (($reader->name == '#text') && $reader->hasValue) { echo '<p>' . $reader->value . '</p>'; } } } } ?>
November 13, 200817 yr Because the contents of the for is pulling out the whole lot, and then you're just repeating that 4 times. Without more info on the XMLReader class you're using, I can't advise how to fix.
November 15, 200817 yr Author What's the XMLReader class? Sorry - I'm a noob! Because the contents of the for is pulling out the whole lot, and then you're just repeating that 4 times. Without more info on the XMLReader class you're using, I can't advise how to fix.
November 15, 200817 yr Author this is my solution, which seems to work nicely $reader = new XMLReader; $res = $reader->open('http://www.blogger.com/feeds/17146895/posts/default'); $entry = false; $i=1; while ($reader->read() && $i<=5) { if ($reader->name == 'entry') { if ($reader->nodeType == XMLReader::ELEMENT) { $entry = true; $i++; } else { $entry = false; } } if ($entry && ($reader->name == 'content')) { $reader->read(); if (($reader->name == '#text') && $reader->hasValue) { echo '<p>' . $reader->value . '</p>'; } } }
Create an account or sign in to comment