You are here:  » selecting just an item or a few of them


selecting just an item or a few of them

Submitted by cesare on Wed, 2006-07-26 17:10 in

Hello David,
is there any easy way to display just a limited number of items (for ex. the first 2 items) out of a feed? And to display just the second (or n) item?

Thank you for your help

Submitted by support on Wed, 2006-07-26 18:05

Hi cesare,

What you need to do is setup a global counter variable $counter, and then increment it within myRecordHandler(); and then study it to determine whether you want to process the current or any more records.

For example, if you just want the first 2 items:

<?php
  $counter 
0;
  function 
myRecordHandler($record)
  {
    global 
$counter;
    
// increment counter
    
$counter++;
    
// process $record here
    
print_r($record);
    
// return TRUE when $counter = 2 to stop reading records
    
return ($counter == 2);
  }
  
MagicParser_parse("file.xml","myRecordHandler");
?>

If you just want the 4th item you would do something like this:

<?php
  $counter 
0;
  function 
myRecordHandler($record)
  {
    global 
$counter;
    
// increment counter
    
$counter++;
    
// we only want the 4th record
    
if ($counter == 4)
    {
      
// process $record here
      
print_r($record);
      
// return TRUE to stop reading any more records
      
return TRUE;
    }
  }
  
MagicParser_parse("file.xml","myRecordHandler");
?>

Cheers,
David.

Submitted by cesare on Fri, 2006-07-28 11:24

It works like a charme, thanks :-)