You are here:  » Magic Parser Online Demo and Source Code Generator Code


Magic Parser Online Demo and Source Code Generator Code

Submitted by kengorden on Fri, 2009-04-03 09:22 in

Hello

I just purchased Magic Parser and love it, it does everything I need it to do.

Quick question, with the Magic Parser Online Demo and Source Code Generator I was able to pull in my feed and page through the results very nicely one at a time.

How is that done? I want to be able to pull back 15 results and display them one at a time

Thanks again,

Kenneth

Submitted by support on Fri, 2009-04-03 11:32

Hi Kenneth,

Thank you for your comments.

The "paging" effect is achieved by virtue of the fact that if you return TRUE from your myRecordHandler function then Magic Parser will stop reading any more records. You simply do this after the record you want to display (based on a "page" parameter in the URL) and ignore all previous records. Here's a simple outline to show what I mean (based on a fixed number of 15 records, with prev and next links):

<?php
  
require("MagicParser.php");
  
$page $_GET["page"]; // get page from the URL
  
if (!$page$page 1// otherwise start on page 1
  
$max 15;
  
$cur 0;
  function 
myRecordHandler($record)
  {
    global 
$page;
    global 
$cur;
    
$cur++;
    if (
$cur $page) return FALSE// skip until we reach the "page" record
    // display the record
    
print_r($record);
    return 
TRUE;
  }
  
MagicParser_parse("filename.xml","myRecordHandler","xml|FORMAT/STRING/");
  print 
"<p>";
  if (
$page 1) print "<a href='?page=".($page-1)."'>Previous</a> ";
  if (
$page $ax) print "<a href='?page=".($page+1)."'>Next</a> ";
  print 
"</p>";
?>

Hope this helps!
Cheers,
David.

Submitted by kengorden on Fri, 2009-04-03 15:25

Thank you David, that worked perfectly! Who knew it was so simple...

The only change I made was to the paging. I changed "$ax" to "$cur" and added "=" so my next link would display on the first record.

<?php
  
if ($page 1) print "<a href='?page=".($page-1)."'>Previous</a> ";
  if (
$page <= $cur) print "<a href='?page=".($page+1)."'>Next</a> ";
?>

My XML feed returns a total record count in the meta data so I changed "15" to the following:

<?php
  $max 
$record["TOTAL"];
?>

Works like a charm.

Thanks again,

Kenneth