To be honest I think I'm in way over my head. I'm trying to get my script to have "Next" and "Previous" links.
"nbresult=10" can be found in the $url and dictates the number of results returned per page from the live feed. I'd like to be able to get the next or previous 10 results.
Would someone please help me to integrate the first "counter script" into the second which is my data feed script (so far) and then I'm done. - Thanks!
<?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");
?>
<?php
header("Content-Type: text/html;charset=utf-8");
require("MagicParser.php");
$url = "http://export.kelkoo.co.uk/ctl/exportSearch?partner=tradedoubler&partnerId=96906467&nbresult=10&offset=1&siteSearchQuery=shoes&catId=100164013";
$numshops = 0;
$numtotalresults = 0;
function myHeaderRecordHandler($record)
{
global $numshops;
global $numtotalresults;
$numshops = $record["HEADER/NUMSHOPS"];
$numtotalresults = $record["HEADER/NUMTOTALRESULTS"];
}
MagicParser_parse($url,"myHeaderRecordHandler","xml|PRODUCTSEARCH/");
print "<p>Num Shops: ".$numshops."</p>";
print "<p>Num Total Records: ".$numtotalresults."</p>";
function myRecordHandler($item)
{
// print "<p>".$item["NUMTOTALRESULTS"]."</p>";
print "<p>".$item["OFFERTITLE"]."</p>";
// print "<p>".$item["MERCHANTNAME"]."</p>";
// print "<p>".$item["MERCHANTURL"]."</p>";
// print "<p>".$item["PRICE"]."</p>";
// print "<p>".$item["COMPAREURL"]."</p>";
// print "<p>".$item["OFFERCATID"]."</p>";
// print "<p>".$item["OFFERCATNAME"]."</p>";
// print "<p>".$item["DELIVERYCOST"]."</p>";
// print "<p>".$item["MEDIUMIMAGE"]."</p>";
// print "<p>".$item["SMALLIMAGE"]."</p>";
}
//View the URL for testing
echo "$url";
// fetch the response and parse the results
MagicParser_parse($url,"myRecordHandler","xml|PRODUCTSEARCH/RESULTLIST/RESULT/");
?>
Dave this is exactly what I wanted and yes I did want $nbresult extracted to a variable. I have a separate configuration file to be included for most of the parameters within the url.
Way above the call of duty, thank you very much!
Here you go - this is made easier by the fact that $url contains the maximum number of results and the offset, so you don't have to do the calculations within myRecordHandler as you've seen in the other paged navigation example.
<?php
header("Content-Type: text/html;charset=utf-8");
require("MagicParser.php");
// get $page from the URL
$page = $_GET["page"];
if (!$page) $page = 1;
$nbresult = 10;
$offset = ((($page-1) * $nbresult)+1);
$url = "http://export.kelkoo.co.uk/ctl/exportSearch?partner=tradedoubler&partnerId=96906467&nbresult=".$nbresult."&offset=".$offset."&siteSearchQuery=shoes&catId=100164013";
$numshops = 0;
$numtotalresults = 0;
function myHeaderRecordHandler($record)
{
global $numshops;
global $numtotalresults;
$numshops = $record["HEADER/NUMSHOPS"];
$numtotalresults = $record["HEADER/NUMTOTALRESULTS"];
}
MagicParser_parse($url,"myHeaderRecordHandler","xml|PRODUCTSEARCH/");
// print "<p>Num Shops: ".$numshops."</p>";
// print "<p>Num Total Records: ".$numtotalresults."</p>";
function myRecordHandler($item)
{
print "<p>".$item["OFFERTITLE"]."</p>";
}
// View the URL for testing
// echo "$url";
// fetch the response and parse the results
MagicParser_parse($url,"myRecordHandler","xml|PRODUCTSEARCH/RESULTLIST/RESULT/");
print "<p>";
if ($page > 1) print "<a href='?page=".($page-1)."'>Prev</a> ";
if (($page * $nbresult) < $numtotalresults) print "<a href='?page=".($page+1)."'>Next</a>";
print "</p>";
?>
I don't know if you're allowed to modify "nbresult" in the Kelkoo URL, however i've extracted it into a variable along with "offset", and then made the paging calculations reference these two variables, so if you want to change the number of results per page you just need to edit $nbresult and it should all work out.
Cheers,
David.