Say i want to loop through XML nodes but i want to ignore the first 10 and then limit the number i grab to 10.
$limit=10; //define results limit
$o=20; //define offset
$i=0; //start line counter
foreach($xml->id AS $key => $value){
$i++;
if($i > $o){
//if line number is less than offset, do nothing.
}else{
if($i == "$limit"){break;} //if line is over limit, break out of loop
//do stuff here
}
}
So in this example, id want to start on result 20, and only show 10 results, then break out of the loop. Its not working though. Any thoughts?
There are multiple bugs in there. It should be
foreach (...
if ($i++ < $o) continue;
if ($i > $o + $limit) break;
// do your stuff here
}
The answer from soulmerge will go through the loop one too many times. It should be:
foreach (...
if ($i++ < $o) continue;
if ($i >= $o + $limit) break;
// do your stuff here
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With