You are here:  » Too Many Record handlers?


Too Many Record handlers?

Submitted by ukdave on Sun, 2007-07-08 21:09 in

Hi David,
I've put together some code (mostly yours) but have a feeling it can be simplified by reducing the number of recordhandlers. Is it possible to use just two recordhandlers here?

<?php
  
require("MagicParser.php");
  
// get $page from the URL
  
$page $_GET["page"];
  if (!
$page$page 1;
  
$prices 20;
  
$startprices = ((($page-1) * $prices)+1);
  
$url "http://my-url-here&query=DC14&show=messages&limit=1&prices=".$prices."&startprices=".$startprices."";
  
$numtotalresults 0;
  function 
myHeaderRecordHandler($record)
  {
  global 
$numtotalresults;
  
$numtotalresults $record["PRODUCT/NUMBER-OF-RETAILERS"];
  print 
"<h4>".$record["NUMBER-OF-RETAILERS"]."</h4>";
  }
  
MagicParser_parse($url,"myHeaderRecordHandler","xml|PRODUCTS/");
  print 
"<p>Num Total Records: ".$numtotalresults."</p>";
  function 
myProductRecordHandler($record)
  {
    global 
$product;
    
$product $record;
  }
  function 
myRetailerRecordHandler($record)
  {
    global 
$ignore;
    if (!isset(
$record["LINK"]))
    {
      
$ignore TRUE;
      return;
    }
    
// process RETAILER record as normal
    
print "<h4>".$record["NAME"]."</h4>";
 
//   print "<h4>".$record["LINK"]."</h4>";
    
print "<h4>".$record["MESSAGE"]."</h4>";
     print 
"<h4>".$record["PRICE"]."</h4>";
  }
  
$ignore FALSE;
  
MagicParser_parse($url,"myProductRecordHandler","xml|PRODUCTS/PRODUCT/");
  
MagicParser_parse($url,"myRetailerRecordHandler","xml|PRODUCTS/PRODUCT/RETAILER/");
  
// check whether to ignore the product receord, process it if not:
  
if (!$ignore)
  {
    
// now process $product just as if it was $record within myProductRecordHandler
      
}
        print 
"Lowest Price: ".$product["LOWEST-PRICE"];
       
// View the URL for testing
  // echo "$url";
  
print "<p>";
  if (
$page 1) print "<a href='?page=".($page-1)."'>Prev</a>&nbsp;&nbsp;";
  if ((
$page $prices) < $numtotalresults) print "<a href='?page=".($page+1)."'>Next</a>";
  print 
"</p>";
  print 
"$numtotalresults";
?>

Thanks.

Submitted by support on Mon, 2007-07-09 06:30

Hi Dave,

If your XML only contains one product, then your code is the most efficient way to do it, as this means you don't have to write the code to loop through multiple child nodes (RETAILER). On the other hand, if your XML contains multiple products, then i'm not sure it will be working properly for you as it stands, because I don't see how it ties the retailer together with the product.

If you are able to email me an example XML document that you are using with the above script i'll take a look for you, and show you how to reduce the number of record handlers if appropriate - which I think from a first glance it will be...

Cheers,
David.

Submitted by ukdave on Mon, 2007-07-09 14:18

Hi David, thanks for that.
Yes, in this instance the XML only contains one product and multiple retailers so thankfully it looks like I got it right this time.

I now have two scripts, the one above which is for product specific queries and another for broader queries such as a manufacturers name.

The problem I have with the code above is that the pagination uses the retailer count in order to work efficiently however once the retailers without a link have been ignored the count is reduced significantly and hence some pages will show 10 out of 10 records while another may show say 6/10.

Would there be much of a performance hit if like before the RETAILER records post checking for links were loaded into an array with a counter in order to correct pagination?

I ask because presumably the whole file would have to be read in advance and as you know there could be hundreds of RETAILERS returned for a given query. I'm thinking this would take a lot more time than requesting say retailers 10 to 20.

What do you think ?

Submitted by support on Mon, 2007-07-09 14:25

Hi Dave,

Your thinking is correct. I only really use pagination when the number of results is returned as a parameter. If this is not given, the only option is to parse the entire file which as you say, may take some time for products with a large number of retailers.

I normally do the same as you have in the above script - simply a "Next" (& "Prev") link, which may occasionally mean that there are no results on the next page, but this only happens if the number of results is exactly divisible by the number of results per page, so it shouldn't happen too often.

Cheers,
David.

Submitted by ukdave on Mon, 2007-07-09 17:08

Hi David,

Having thought about everything you have said I have decided to use the above code as it is.

I found that the other code you helped me with (multiple products/single retailer) runs faster the more I reduce the amount of products requested.

I suppose if I was to use a mysql database records could be retreaved and sorted quite quickly. Problem is that those records would still need to be loaded in the first place which takes time and with a live feed it may not take long for links to become invalid which is why I try to avoid using a cache for longer than 24hrs. At least think these are the pit falls.

Thanks again!