You are here:  » Limit the number of items displayed


Limit the number of items displayed

Submitted by murph on Wed, 2006-10-04 14:03 in

I am trying to figure out how to get magicparser to limit the number of items displayed...

is it possible?

Submitted by support on Wed, 2006-10-04 14:13

Hi murph,

It's no problem - Magic Parser will stop reading any more records if your record handler function returns TRUE. Therefore, all you need to do is setup a global counter and increment it for each record - then return true if the counter has reached the number of items you want to display. For example, to process 3 items only:

<?php
  
require("MagicParser.php");
  
$counter 0;
  function 
myRecordHandler($item)
  {
    global 
$counter;
    
// process $item as required
    // increment record counter
    
$counter++;
    return (
$counter == 3);
  }
  
MagicParser_parse("file.xml","myRecordHandler");
?>

Hope this helps!
Cheers,
David.

Submitted by murph on Wed, 2006-10-04 14:35

Thanks David :)

Submitted by function on Sun, 2008-01-13 12:25

I have a question on how to merge that code with this, so I can limit the number of items:

<?php
  require("MagicParser.php");
  function myRecordHandler($record)
  {
    print "<tr><td><img src=\"".$record["AUTN:CONTENT/DOCUMENT/IMAGE"]."\"></td>";
    print "<td>".$record["AUTN:TITLE"]."</td></tr>";
  }
  $url = "the-xmlfeed-i-use";
  MagicParser_parse($url,"myRecordHandler","xml|AUTNRESPONSE/RESPONSEDATA/AUTN:HIT/")
?>

Thanks,
Tro

Submitted by support on Sun, 2008-01-13 12:39

Hi Tro,

Here's the limit records code merged with your example:

<?php
  
require("MagicParser.php");
  
$counter 0;
  function 
myRecordHandler($record)
  {
    global 
$counter;
    print 
"<tr><td><img src=\"".$record["AUTN:CONTENT/DOCUMENT/IMAGE"]."\"></td>";
    print 
"<td>".$record["AUTN:TITLE"]."</td></tr>";
    
$counter++;
    return (
$counter == 3);
  }
  
$url "the-xmlfeed-i-use";
  
MagicParser_parse($url,"myRecordHandler","xml|AUTNRESPONSE/RESPONSEDATA/AUTN:HIT/")
?>

Cheers,
David.