0

Is there a easy way to separate the output from SimpleXML into pages? Let's say the XML file got 200 elements but I want to split it into 20 elements on each page. So it would be like index.php?page=1?

$xml = new SimpleXMLElement(file_get_contents("demofile.xml"));
$per_no = $xml->children();
echo count($per_no) . ' are now active<br />';
$cnt = 0;
foreach ($xml->xpath('/webcams_online/webcam') as $node) {
    if ($cnt == 10) {
        break;
    }
    $itemXML = array(
        'account'         => $node['account'],
        'nickname'        => $node['nickname'],
        'number_visitors' => $node['number_visitors']
    );
    $cnt++;
    ?>
    <?php echo $itemXML['account']; ?>
<?php
}
hakre
  • 193,403
  • 52
  • 435
  • 836
Bulfen
  • 97
  • 2
  • 9
  • what is a "page" in your XML? Show some example please. In any case, `array_chunk` will probably work, as would a simple `foreach` or a `LimitIterator` – Gordon Feb 05 '12 at 15:09
  • Sorry for not explaining so good. I mean that for each element I "echo out" should be max 20 per page. So the 20 next would be on page 2 and 3 and so on. – Bulfen Feb 05 '12 at 15:15
  • 1
    that still doesnt explain which element makes up a page, but the answer is one of the three options I gave in the first comment. add `array_slice` to the list. You can also do it via XPath's position() function. – Gordon Feb 05 '12 at 15:20
  • Thank you for the reply Gordon. I need to look into array_slice – Bulfen Feb 05 '12 at 15:26
  • possible duplicate of [XML Feeds & PHP - Limit the number of items](http://stackoverflow.com/questions/7876931/xml-feeds-php-limit-the-number-of-items) – Gordon Feb 05 '12 at 15:26
  • 1
    Please refer to this... http://stackoverflow.com/questions/7876931/xml-feeds-php-limit-the-number-of-items I hope this is what you want...! – Fahid Mohammad Feb 05 '12 at 15:16
  • Thank you Fahid Mohammad. Seems like it's the same as I wanna do. Need to test it out. :) – Bulfen Feb 05 '12 at 15:26
  • @Gordon It's allmost the same as the "Limit the number of items" but I just don't wanna limit the the number of items from start => 10. I also want to be able to put out items 10-20 and 20-30 and so on. If that made any more sense. – Bulfen Feb 05 '12 at 16:38
  • 2
    you can combine criteria with `and` and `or` so you can do `position() > 10 and position() < 20` – Gordon Feb 05 '12 at 17:14
  • @Gordon Thank you. Gonna look into it. :) – Bulfen Feb 05 '12 at 17:16

0 Answers0