You are here:  » Next record


Next record

Submitted by websitings on Tue, 2006-12-19 11:54 in

I am parsing my feed file ok and returning the first record. Not being great with php I am struggling to implement a Next link which when clicked would display the next record, any assistance would e greatly appreciated.

<?php
  
  
require("MagicParser.php");
  
$filename "http://127.0.0.1//parser/magazania_titles.txt";
  
$format_string MagicParser_getFormat($filename);
  if (!
$format_string)
  {
    print 
"<p>".MagicParser_getErrorMessage()."</p>";
    exit;
  }
  function 
myRecordHandler($record)
  {global 
$counter;
    
// increment counter
    
$counter++;
    print 
"<table border='1'>";
    foreach(
$record as $key => $value)
      {
      print 
"<tr>";
      print 
"<th>".$key."</th>";
      print 
"<td>".htmlentities($value)."&nbsp;
      </td>"
;
      print 
"</tr>";
    }
    print 
"</table>";
    return (
$counter == 1);
  }
  
MagicParser_parse($filename,"myRecordHandler",$format);
?>

Submitted by support on Tue, 2006-12-19 12:10

Hi,

Here's a modified version of your code above that uses a page variable to loop through the records until you reach the one you want to display, together with a "Next" link to the next record (if you have not reached the end).

I've explained the additional code in the comments....

<?php
  
require("MagicParser.php");
  
$filename "http://127.0.0.1//parser/magazania_titles.txt";
  
$format_string MagicParser_getFormat($filename);
  if (!
$format_string)
  {
    print 
"<p>".MagicParser_getErrorMessage()."</p>";
    exit;
  }
  
// get the current page from the URL
  
$page $_GET["page"];
  
// if not set, force to page 1
  
if (!$page$page 1;
  function 
myRecordHandler($record)
  {
    global 
$counter;
    global 
$page;
    
// increment counter
    
$counter++;
    
// return false if counter not yet reached page to view
    
if ($counter $page) return false;
    print 
"<table border='1'>";
    foreach(
$record as $key => $value)
      {
      print 
"<tr>";
      print 
"<th>".$key."</th>";
      print 
"<td>".htmlentities($value)."&nbsp;
      </td>"
;
      print 
"</tr>";
    }
    print 
"</table>";
    
// return true to stop parsing
    
return true;
  }
  
MagicParser_parse($filename,"myRecordHandler",$format);
  
// if possibly more records, create link to next record
  // otherwise print end of file message
  
if ($counter == $page)
  {
    print 
"<p><a href='?page=".($page+1)."'>Next</a></p>";
  }
  else
  {
    print 
"<p>No more records.</p>";
  }
?>

Hope this helps!
Cheers,
David.

Submitted by websitings on Tue, 2006-12-19 13:49

Wow, the support for this script is amazing.
Thanks David it worked a charm.
Bill