You are here:  » How to page the Magicparser results


How to page the Magicparser results

Submitted by travelfrog on Mon, 2006-05-01 17:23 in

I have created my first page to display an xml feed. I have also managed to display a set number of results using help from a script which I found on this forum.

Instead of displaying 10 results, I would like to display all 90 results, but paged to display only 10 per page. Can anyone point me in the right direction?

Here is the code that I am currently using to return 10 results ($item):-

<?php
//require("MagicParser.php");
require $_SERVER['DOCUMENT_ROOT'].'/magic-parser/MagicParser.php';
$counter 0;
function 
myRecordHandler($item)
{
global 
$counter;
// process $item as normal
print "
<tr>
  <td align=\"center\" width=\"200\" height=\"150\">
  <a href='"
.$item["PRODUCTURL"]."'><img src='".$item["IMAGEURL"]."' height='110' width='150' style='border-style: none'></a>
  </td>
  <td width=\"410\" height=\"150\">
    <p>
    <strong>Hotels - hotel:</strong> <a href='"
.$item["PRODUCTURL"]."'><strong>".$item["NAME"]."</strong></a>
    </p>
    <br />
    <p>
    <a href='"
.$item["PRODUCTURL"]."'><strong>".$item["PRICE"]."".$item["CURRENCY"]."</strong> <em>per night</em></a>
    <br />
    </p>
    <br />
    <p>
    "
.$item["DESCRIPTION"]."
    </p>
  </td>
</tr>
<tr>
  <td align=\"center\" colspan=\"2\">
    <span class=\"content-text-bold\">- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -</span>
  </td>
</tr>"
;
// incremement $counter
$counter++;
// if $counter = 10, return true
return ($counter == 10);
}
//Print the table open tag
print "<table class=\"content-text\" width=\"610\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">";
//Parse the xml feed
MagicParser_parse("http://my-feed-url-is-inserted-here","myRecordHandler","xml|PRODUCTS/PRODUCT/");
//Print the table close tag
print "</table>";
?>

Submitted by support on Tue, 2006-05-02 08:28

Hi,

The way to do this would be have a second counter for the number of records to skip based on the current page. Then, you simply return from the record handler function whilst counting up to the point at which display should begin for the current page. Here is some outline code to demonstrate what I mean:

<?php
  $page 
$_GET["page"];
  
$counter 0
  
$itemsOnPage 10;
  
// default to the first page
  
if (!$page$page 1;
  function 
myRecordHandler($item)
  {
    global 
$page;
    global 
$counter;
    global 
$itemsOnPage;
    
$counter++;
    
// return false whilst parsing items on previous pages
    
if ($counter < (($page-1)*$itemsOnPage)) return false;
    
// display the item
    
print_r($item);
    
// return true if counter reaches current page * items on each page
    
return ($counter == ($page*$itemsOnPage));
  }
  
// display link to next page
  
print "<p><a href='?page=".($page+1)."'>Next</a></p>";
  
// parse the xml feed
  
MagicParser_parse("feed.xml","myRecordHandler");
?>

Submitted by travelfrog on Tue, 2006-05-02 11:47

Thank you for the reply. I have inserted your paging script and it works ok except that the first page is displaying the correct 10 items, but the next pages are displaying 11 items. My code is as below:-

<?php
//require("MagicParser.php");
require $_SERVER['DOCUMENT_ROOT'].'/magic-parser/MagicParser.php';
$page $_GET["page"];
$counter 0;
$itemsOnPage 10;//Set the number of items displayed per page
// default to the first page
if (!$page$page 1;
function 
myRecordHandler($item)
{
global 
$page;
global 
$counter;
global 
$itemsOnPage;
$counter++;
// return false whilst parsing items on previous pages
if ($counter < (($page-1)*$itemsOnPage)) return false;
// process $record as normal
print "
<tr>
    <td align=\"center\" width=\"200\" height=\"150\">
        <a href='"
.$item["PRODUCTURL"]."'><img src='".$item["IMAGEURL"]."' height='110' width='150' style='border-style: none'></a>
    </td>
    <td width=\"410\" height=\"150\">
        <p>
        <strong>Hotels - hotel:</strong> <a href='"
.$item["PRODUCTURL"]."'><strong>".$item["NAME"]."</strong></a>
        </p>
        <br />
        <p>
        <a href='"
.$item["PRODUCTURL"]."'><strong>".$item["PRICE"]."".$item["CURRENCY"]."</strong> <em>per night</em></a>
        <br />
        </p>
        <br />
        <p>
        "
.$item["DESCRIPTION"]."
        </p>
    </td>
</tr>
<tr>
    <td align=\"center\" colspan=\"2\">
    <span class=\"content-text-bold\">- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -</span>
    </td>
</tr>"
;
// return true if counter reaches current page * items on each page
return ($counter == ($page*$itemsOnPage));
}
// display link to next page
print "<p><a href='?page=".($page+1)."'><h1 align=\"center\">Next</h1></a></p>";
//Print the table head tag
print "<table class=\"content-text\" width=\"610\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">";
//Parse the xml feed
MagicParser_parse("my-url-is-here","myRecordHandler","xml|PRODUCTS/PRODUCT/");
//Print the table foot tag
print "</table>";
?>

I have already been using a pagination script on my pages that retrieve from a Mysql database, but I could not get it to work. How can I add previous, next, the number of pages, display the total amount of results and sort by lowest price? Any help would be appreciated. Below is the script that I normally use for Mysql pagination.

<?php
// If current page number, use it if not, set one!
if(!$_GET['page'])$page 1;
else 
$page $_GET['page'];
// Define the number of results per page
$max_results 10;
//Max amount of pages
$max_pages=15;
// Figure out the limit for the query based on the current page number.
$from = (($page $max_results) - $max_results);
$result mysql_query("select * from XXX where X=Y ORDER BY 'X' desc LIMIT $from$max_results");
while(
$r=mysql_fetch_array($result))
{
Display the table here
}
// Figure out the total number of results in DB:
$total_results mysql_result(mysql_query("SELECT COUNT(*) as Num FROM XXX WHERE X=Y"),0);
//No. of Page number links at bottom
if (ceil($total_results $max_results) < $max_pages)
{
$no_of_page_numbers ceil($total_results $max_results);
}
else
{
$no_of_page_numbers $max_pages;
}
// Figure out the total number of pages. Always round up using ceil()
$total_pages ceil($total_results $max_results);
echo 
"<center><span class=\"content-text-bold\">Found $total_results Properties.</span><br />";
echo 
"<span class=\"content-text-bold\">Now showing page $page of $total_pages pages.</span></center><br />";
if(
$page 1) echo "<a class=\"contentlink\" href='".$_SERVER['PHP_SELF']."?page=".($page 1)."'><< Previous</a>";
$start $page-ceil((($no_of_page_numbers-1)/2));
$stop $page+ceil((($no_of_page_numbers-1)/2));
if(
$start<=1)
{
$start=1$stop $no_of_page_numbers;
}
elseif(
$stop>=$total_pages)
{
$start=($total_pages-$no_of_page_numbers); $stop $total_pages;
}
for(
$i $start$i <= $stop$i++)
{
if(
$page==$i) echo "<center><span class=\"content-text-bold\">$i | </span>";
else echo 
"<a class=\"contentlink\" href='".$_SERVER['PHP_SELF']."?page=$i'>$i</a> | ";
}
if(
$page $total_pages) echo "<a class=\"contentlink\" href='".$_SERVER['PHP_SELF']."?page=".($page 1)."'>Next &gt;&gt;</a></center>";
}
?>