Hi 1st question from me for the board Trying to limit the number of items displayed from an XML feed using simplexml: $xml = simplexml_load_file('feed.xml'); foreach ($xml->channel->item as $item) PHP: I have tried: $item = array_slice($xml->item, 0, 3); PHP: which gives an error and $count = 1; for ($x = 0; $x < $count; $x++); PHP: Which shows the last result from the feed. Any ideas where I am going wrong - ideally I just want display the 1st item, but am looking for something that could display 1, 2, 3 items etc... Help muchos appreciated.
A little crude but you could simple add a counter and if() statement to break out of the loop depending on the counter value $cnt = 0; for($items as $item){ // your code // some condition if(++$cnt == 1) break; } PHP:
Cheers mrmonster , I tried your suggestion, code looks like: <?php $cnt = 0; for($items as $item) { $xml = simplexml_load_file('feed.xml'); foreach ($xml->channel->item as $item); if(++$cnt == 1) break; } { echo " "; echo " "; } ?> PHP: Unfortunately it did not work - is there a problem with the syntax here - i am still learning with PHP and am a relative noob
Do you mean this? <?php $cnt = 0; $xml = simplexml_load_file('feed.xml'); foreach ($xml->channel->item as $item){ if(++$cnt == 1){ break; } } ?> PHP:
Your logic doesn't seem right here, you have a loop within a loop and both of them are creating $item. This might make it a little simpler, it will loop max_items number of times and exit, assuming your $xml object is valid. $cnt = 0; $max_items = 1; $xml = simplexml_load_file('feed.xml'); foreach ($xml->channel->item as $item){ // insert code to process your $item here if(++$cnt == $max_items) break; } PHP: