You are here:  » Fatal error: Cannot redeclare catalogue_handler()

Support Forum



Fatal error: Cannot redeclare catalogue_handler()

Submitted by howardtheblack on Thu, 2007-11-29 21:14 in

I have a set of XML files which I wish to parse. However, the xml files look like the following:

1st xml

<?xml version="1.0" encoding="utf-8" ?>
<regions>
<region>Albania</region>
<region>Algeria</region>
</regions>

From the data in between the <region> tag, I'll be able to find the xml file to its corresponding region by going to http://www.test.com/xml/region/{REGION}. eg. http://www.test.com/xml/region/Albania

Now in the region specific xml, I can find the product codes and will be able to use that code to retrieve the detailed product information by using similiar rules as the region.

The file from http://www.test.com/xml/region/Albania looks like:

<?xml version="1.0" encoding="utf-8" ?>
<inventory>
<product>
<PCode>WSO</PCode>
</product>
<product>
<PCode>SEK</PCode>
</product>
<product>
<PCode>JRS</PCode>
</product>
</inventory>

I then first store all the regions from the first xml file into the database and came up with the following code to get all the product codes:

<?php
require("MagicParser.php");
require(
"db.php");
// retrieve regions from database
$regions_query "SELECT * FROM regions";
$regions_result mysql_query($regions_query) or die("Fail to retrieve entries from regions");
while (
$regions_row mysql_fetch_assoc($regions_result))
{
    
$region $regions_row['region'];
    
// assign xml source
     
$catalogue_url "http://www.test.com/xml/region/".$region;
    
// define xml elements to parse
    
$catalogue_parse "xml|INVENTORY/PRODUCT/";
    
// fetch the xml file
    
$catalogue_filename cacheFetch($catalogue_url0);
    function 
catalogue_handler($catalogue_record)
    {
        
$productcode $catalogue_record["PCODE"];
        
$productcode_found 0;
        
// check if the productcode is already in the database entry
        
$productcode_query "SELECT * FROM productcodes";
        
$productcode_result mysql_query($productcode_query) or die("Fail to retrieve entries from productcodes to check");
        while (
$productcode_row mysql_fetch_assoc($productcode_result))
        {
            
$productcode_entry $productcode_row['productcode'];
            
// if the productcode is found in the database, set $productcode_found to 1
            
if($productcode_entry == $productcode)
            {
                
$productcode_found 1;
            }
        }
        
// if productcode entry not found, insert productcode into database
        
if($productcode_found == 0)
        {
            
// update database entry
            
$catalogue_query "INSERT INTO productcodes (productcode) VALUES ('$productcode')";
            
mysql_query($catalogue_query) or die("Fail to update entries under productcodes");
        }
    }
    
MagicParser_parse($catalogue_filename"catalogue_handler"$catalogue_parse);
}
mysql_close($dbh);
?>

However, I was getting "Fatal error: Cannot redeclare catalogue_handler()" message. Is there a way to get around it? Please help.

Submitted by support on Thu, 2007-11-29 21:19

Hi,

The problem here is because you have the code for your record handler function within your while() loop; so PHP tries to declare the function and can't - because it was already declared on the last iteration.

This should just be a case of moving the function outside of the loop, for example:

<?php
require("MagicParser.php");
require(
"db.php");
// declare catalogue_handler() function here, outside of the while() loop...
    
function catalogue_handler($catalogue_record)
    {
        
$productcode $catalogue_record["PCODE"];
        
$productcode_found 0;
        
// check if the productcode is already in the database entry
        
$productcode_query "SELECT * FROM productcodes";
        
$productcode_result mysql_query($productcode_query) or die("Fail to retrieve entries from productcodes to check");
        while (
$productcode_row mysql_fetch_assoc($productcode_result))
        {
            
$productcode_entry $productcode_row['productcode'];
            
// if the productcode is found in the database, set $productcode_found to 1
            
if($productcode_entry == $productcode)
            {
                
$productcode_found 1;
            }
        }
        
// if productcode entry not found, insert productcode into database
        
if($productcode_found == 0)
        {
            
// update database entry
            
$catalogue_query "INSERT INTO productcodes (productcode) VALUES ('$productcode')";
            
mysql_query($catalogue_query) or die("Fail to update entries under productcodes");
        }
    }
// retrieve regions from database
$regions_query "SELECT * FROM regions";
$regions_result mysql_query($regions_query) or die("Fail to retrieve entries from regions");
while (
$regions_row mysql_fetch_assoc($regions_result))
{
    
$region $regions_row['region'];
    
// assign xml source
     
$catalogue_url "http://www.test.com/xml/region/".$region;
    
// define xml elements to parse
    
$catalogue_parse "xml|INVENTORY/PRODUCT/";
    
// fetch the xml file
    
$catalogue_filename cacheFetch($catalogue_url0);
    
MagicParser_parse($catalogue_filename"catalogue_handler"$catalogue_parse);
}
mysql_close($dbh);
?>

Hope this helps,
Cheers,
David.

Submitted by howardtheblack on Thu, 2007-11-29 21:39

Works fine now. Hehe, was getting a bit too excited with this parser and forgot the handler is acyually just a function. Thank you very much for the help. :D