I am using MagicParser on
{LINK SAVED}, but the datafeed is just coming out in a text format, what should I do?
Thanks for that, I am beginning to see how it all works. Please bear with me as I am a novice at this! Some of the feeds I am using have hundreds or thousands of products can I build in a limit to the number of products on the page, i.e. 12 plus "previous page" and "next page" links to the rest of the products.
Hi,
Paging is possible, and can be done quite easily because Magic Parser allows you to return TRUE from your record handler function in order to stop reading any more records. You just have to combine this with a bit of code to skip through records prior to the "page" you want to display, and then create links to the previous and next pages.
Based on the above record handler function in the example, here is a bit more code that will display a max number of records with previous and next links. I've added the call to MagicParser_parse(), but of course you will need to replace that line with your existing code.
<?php
require("MagicParser.php");
// get the current page from the URL
$page = $_GET["page"];
// if not set, force to page 1
if (!$page) $page = 1;
// reset counter, and define number of items wanted on each page
$counter = 0;
$itemsOnPage = 10;
function myRecordHandler($record)
{
global $page;
global $counter;
global $itemsOnPage;
// increment counter
$counter++;
// don't display while looping through items on previous pages
if ($counter < (($page-1)*$itemsOnPage)) return FALSE;
// stop parsing if already displayed maxium number of items per page
if ($counter == ($page*$itemsOnPage)) return TRUE;
// display record as normal
print "<table border='1'>";
print "<tr><th>Product:</th><td>".$record["NAME"]."</td></tr>";
print "<tr><th>Description:</th><td>".$record["DESCRIPTION"]."</td></tr>";
print "<tr><td colspan='2'><a href='".$record["PRODUCTURL"]."'>Click here to buy now</a></td></tr>";
print "</table>";
print "<br />";
}
MagicParser_parse(...); // replace with your existing MagicParser_parse() code
// finally display next and previous links
if ($page > 1)
{
print "<p><a href='?page=".($page-1)."'>Prev</a></p>";
}
print "<p><a href='?page=".($page+1)."'>Next</a></p>";
?>
Hope this points you in the right direction!
Cheers,
David.
Brilliant on the first page, but when I clicked on next I got an error message relating to the line containing 'if ($counter
Ooops,
I lifted the paging code from a foreach loop rather than a function; the following code:
// don't display while looping through items on previous pages
if ($counter < (($page-1)*$itemsOnPage)) continue;
...should actually be:
// don't display while looping through items on previous pages
if ($counter < (($page-1)*$itemsOnPage)) return FALSE;
I've corrected it in the above code as well...
Hope this helps,
Cheers,
David.
Thanks for that.
It all works fine now.
Kind regards
Stephen
David
I have made a more thorough check to the end of the datafeed, but the 'next' link seems to appear to keep going beyond the feed data available. For instance if a feed covers 5 pages there will be a 'next' link to page 6 and on that a 'next' link to page 7 etc.
Yours
Stephen
Hi Stephen,
Because parsing XML is a serial process, you don't actually know how many records there are in the file until you have reached the end. This makes removing the "Next" link if there are no more pages somewhat inefficient, although it is possible to do it by first counting the records, and then carrying on as before.
There is one good optimisation however - only count the records on the first page, and pass the count through to subsequent pages on the URL. Based on the example above, have a look at the following modification to see what I mean. Don't forget to replace "filename.xml" and "xml|FORMAT/STRING/" with your actual values.
<?php
require("MagicParser.php");
// get the current page and total from the URL
$page = $_GET["page"];
$total = $_GET["total"];
// if not set, force to page 1
if (!$page) $page = 1;
// count the records if not in URL
function myRecordCountHandler($record)
{
global $total;
$total++;
}
if (!$total)
{
MagicParser_parse("filename.xml","myRecordCountHandler","xml|FORMAT/STRING/");
}
// reset counter, and define number of items wanted on each page
$counter = 0;
$itemsOnPage = 10;
function myRecordHandler($record)
{
global $page;
global $counter;
global $itemsOnPage;
// increment counter
$counter++;
// don't display while looping through items on previous pages
if ($counter < (($page-1)*$itemsOnPage)) return FALSE;
// stop parsing if already displayed maxium number of items per page
if ($counter == ($page*$itemsOnPage)) return TRUE;
// display record as normal
print "<table border='1'>";
print "<tr><th>Product:</th><td>".$record["NAME"]."</td></tr>";
print "<tr><th>Description:</th><td>".$record["DESCRIPTION"]."</td></tr>";
print "<tr><td colspan='2'><a href='".$record["PRODUCTURL"]."'>Click here to buy now</a></td></tr>";
print "</table>";
print "<br />";
}
MagicParser_parse("filename.xml","myRecordHandler","xml|FORMAT/STRING/");
// finally display next and previous links
if ($page > 1)
{
print "<p><a href='?total=".$total."&page=".($page-1)."'>Prev</a></p>";
}
if ($counter < $total)
{
print "<p><a href='?total=".$total."&page=".($page+1)."'>Next</a></p>";
}
?>
Hope this helps!
Cheers,
David.
David
I'm going to be a pain again!
The code isn't working today for any MagicParser pages, but Price Tapestry is processing the same files as normal.
There is no code being processed and the source code for the pages have no content for the MagicParser code section of the page.
All I have done is changed the permission on the folder and files to allow the PT automation script access and that is still processing the data from the feeds.
Stephen
Hi Stephen,
This does sound like a permission error. What I would suggest is printing out the Magic Parser error message at the end of your script - this should help diagnose the problem:
<?php
print "<p>".MagicParser_getErrorMessage()."</p>";
?>
If it is a "failed to open ..." error (which is most likely), the first thing to look at is what action you took to enable write access to the automation script. You can always try a test script running in the same folder as your Magic Parser code to test access:
<?php
if (is_readable("path/to/filename.xml"))
{
print "File access OK!";
}
else
{
print "Cannot access file!";
}
?>
Hope this helps,
Cheers,
David.
David
All I get is a parse error for the 2nd line, even after I corrected the spelling of readable!
Stephen
Ooops, sorry - a couple of brackets missing! I've corrected the code above...
Cheers,
David.
David
Still doesn't work, even if I set it to '777' on Unix.
Stephen
David
It seems to have cleared now, with Unix '766', both MP and PT are parsing now.
Thanks
Stephen
David
I am now adding the code to the pages, but they now seem to be ignoring the 'next page' system and display all the products from the feed in them, as at http://www.the-big-business-directory.com/uk_directory/home_and_garden/furnishings/furniture/outdoors/arboreta/index.php, http://www.the-big-business-directory.com/uk_directory/sex/sex_toys_and_adult_products/ann_summers/index.php etc.
Yours
Stephen
My mistake, miscopied the code, my mistake.
Sorry
Stephen
Hi,
The output that you are seeing is the output from PHP's print_r() function, so the parsing is working correctly. If you want to display the contents in a table, similar to the demo script on this website you would need to write code to generate the table, and display each element of the array as required.
The second thing you need to consider is how you want to page the output. Considering just one record, you would need to change the code within your record handler function as follows:
<?php
function myRecordHandler($record)
{
print "<table border='1'>";
foreach($record as $k => $v)
{
print "<tr>";
print "<th>".$k."</th>";
print "<td>".$v."</td>";
print "</tr>";
}
print "</table>";
print "<br />";
}
?>
If you only want to display certain fields, then your code might look like this:
<?php
function myRecordHandler($record)
{
print "<table border='1'>";
print "<tr><th>Product:</th><td>".$record["NAME"]."</td></tr>";
print "<tr><th>Description:</th><td>".$record["DESCRIPTION"]."</td></tr>";
print "<tr><td colspan='2'><a href='".$record["PRODUCTURL"]."'>Click here to buy now</a></td></tr>";
print "</table>";
print "<br />";
}
?>
Hope this helps!
Cheers,
David.