limiting displayed items

Discussion in 'PHP' started by j1n, May 31, 2007.

  1. #1
    Hi 1st question from me for the board :D

    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.
     
    j1n, May 31, 2007 IP
  2. mrmonster

    mrmonster Active Member

    Messages:
    374
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #2
    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:
     
    mrmonster, May 31, 2007 IP
  3. j1n

    j1n Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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 :confused:
     
    j1n, May 31, 2007 IP
  4. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #4
    Do you mean this?
    <?php    
    	$cnt = 0;
    	$xml = simplexml_load_file('feed.xml');    
    	foreach ($xml->channel->item as $item){
    		if(++$cnt == 1){
    			break;
    		}
    	}
    ?>
    PHP:
     
    dp-user-1, May 31, 2007 IP
  5. mrmonster

    mrmonster Active Member

    Messages:
    374
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #5


    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:
     
    mrmonster, May 31, 2007 IP
  6. j1n

    j1n Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    k+ to you mrmonster :cool:

    That last snippet fixxed it.

    Working now - DANKE! :D
     
    j1n, May 31, 2007 IP