PHP simplexml retrieve first n records in xml

151
February 02, 2018, at 00:44 AM

i've just started using simplexml and i have the following statement that displays all the records in the xml:

$xml=simplexml_load_file("data.xml") or die("Error: Cannot create object");
foreach($xml->children() as $rec) { 
    echo $rec->dist . "<br />";
}

For pagination purposes, how would I go about retrieving a range of records from the xml, e.g. just the first 10 records (0-9)?

thx in advance!

Answer 1

You could try something like this :

$children = $xml->children() ;
$num = count($children) ;
$start = 10 ; // zero-based index
$end = 20 ; // zero-based index
for ($i = $start; $i < $end ; $i++) {
    echo $children[$i]->dist . "<br />";
}

Previous version :

$line = 1 ;
foreach($xml->children() as $rec) { 
    echo $rec->dist . "<br />";
    if ($line++ == 10) break;
}

This will show 10 first elements.

READ ALSO
foreach until find some value

foreach until find some value

I have a code that get categories from database but I don't know how to get all subcategories(parents)

178
Getting values from an array throws ErrorException &ldquo;Trying to get property of non-object&rdquo;

Getting values from an array throws ErrorException “Trying to get property of non-object”

I am trying to display the keys for each event in an array

178
uploading data from excel in php

uploading data from excel in php

i have uploaded a data from excel to the database using php , the problem is that inside my database table instead that the data was separated they are joined in one output in db

178
Microsoft CSP Partner Center REST API in PHP get token

Microsoft CSP Partner Center REST API in PHP get token

The token is the key generated when set up API access in Partner Center ?

794