You are here:  » Global Help


Global Help

Submitted by crounauer on Fri, 2006-08-25 11:47 in

Hi David,

I am trying to set up a global from one set of parse results to use in another parser lower down on the same page.

I.e. for each country it should should display the relevant region(counties) in the second parser by using the country code from the first.

I know how to make globals using $_GET when sending info to another page, but am unsure about using it on the same page.

<?php
// Parse the countries start
    
function myCountryHandler($country)
  {
      print 
"".$country["COUNTRY_CODE"]."<br>";
      print 
"".$country["COUNTRY_NAME"]."<br><br>";
  }
  
$country_url "http://xml.laterooms.com/xml_ctry.php3";
  
MagicParser_parse($country_url,"myCountryHandler","xml|CTRY_LIST/COUNTRY/");
// Parse the countries end
// Parse the regions start
  
function myRegionHandler($region)
  {
  foreach(
$region as $key => $value)
    {
      print 
"".$region["REGION_CODE"]."<br>";
      print 
"".$region["REGION_COUNTRY"]."<br>";
      print 
"".$region["REGION_DESCRIPTION"]."<br><br>";
  }
  
$region_url "http://xml.laterooms.com/xml_region.php3?ctry=1&lang=eng";
  
MagicParser_parse($region_url,"myRegionHandler","xml|REGION_LIST/REGION/");
// Parse the regions end
?>

Thanks,
Simon

Submitted by crounauer on Fri, 2006-08-25 12:00

Hi,

Think I have to declare the whole array global?

<?php
global $array;
$array[] = $country;
?>

Am I on the right track?

Submitted by support on Fri, 2006-08-25 12:02

Hi Simon,

All you need to do is declare two global variables $country_code and $country_name in each function; and then set them in the first record handler and refer to them in the second. For example:

<?php
  
// Parse the countries start
  
function myCountryHandler($country)
  {
      global 
$country_code;
      global 
$country_name;
      
$country_code $country["COUNTRY_CODE"];
      
$country_name $country["COUNTRY_NAME"];
      print 
"".$country["COUNTRY_CODE"]."<br>";
      print 
"".$country["COUNTRY_NAME"]."<br><br>";
  }
  
$country_url "http://xml.laterooms.com/xml_ctry.php3";
  
MagicParser_parse($country_url,"myCountryHandler","xml|CTRY_LIST/COUNTRY/");
// Parse the countries end
// Parse the regions start
  
function myRegionHandler($region)
  {
      global 
$country_code;
      global 
$country_name;
      print 
"".$country_code."<br>";
      print 
"".$country_name."<br>";
      print 
"".$region["REGION_CODE"]."<br>";
      print 
"".$region["REGION_COUNTRY"]."<br>";
      print 
"".$region["REGION_DESCRIPTION"]."<br><br>";
  }
  
$region_url "http://xml.laterooms.com/xml_region.php3?ctry=1&lang=eng";
  
MagicParser_parse($region_url,"myRegionHandler","xml|REGION_LIST/REGION/");
// Parse the regions end
?>

Hope this helps!
Cheers,
David.

Submitted by support on Fri, 2006-08-25 12:04

Ah - I may have misunderstood you...

Is REGION_COUNTRY in the second feed equivalent to the COUNTRY_CODE in the first feed; and you want to display the associated country?

Submitted by support on Fri, 2006-08-25 12:11

Here's the code to do that (if that's the case - it makes more sense!)...

<?php
  
// Create a global array to hold countries indexed by code
  
$countries = array();
  
// Parse the countries start
  
function myCountryHandler($country)
  {
      global 
$countries;
      
$countries[$country["COUNTRY_CODE"]] = $country["COUNTRY_NAME"];
  }
  
$country_url "http://xml.laterooms.com/xml_ctry.php3";
  
MagicParser_parse($country_url,"myCountryHandler","xml|CTRY_LIST/COUNTRY/");
// Parse the countries end
// Parse the regions start
  
function myRegionHandler($region)
  {
      global 
$countries;
      print 
"".$region["REGION_CODE"]."<br>";
      print 
"".$region["REGION_COUNTRY"]."<br>";
      print 
"".$countries[$region["REGION_COUNTRY"]]."<br>";
      print 
"".$region["REGION_DESCRIPTION"]."<br><br>";
  }
  
$region_url "http://xml.laterooms.com/xml_region.php3?ctry=1&lang=eng";
  
MagicParser_parse($region_url,"myRegionHandler","xml|REGION_LIST/REGION/");
// Parse the regions end
?>

Cheers,
David.

Submitted by crounauer on Fri, 2006-08-25 12:20

Thanks David,

The script below works great for a single country. Think I need to use the "foreach" command to get the same results, but for all the countries in this url

<?php
http
://xml.laterooms.com/xml_ctry.php3
?>

<?php
// Parse the countries start
    
function myCountryHandler($country)
  {
    
//global $array;
    //$array[] = $country;
    
global $country_code;
    global 
$country_name;
    
$country_code $country["COUNTRY_CODE"];
    
$country_name $country["COUNTRY_NAME"];
    
//print "".$country["COUNTRY_CODE"]."<br>";
    //print "".$country["COUNTRY_NAME"]."<br><br>";
  
}
  
$country_url "http://xml.laterooms.com/xml_ctry.php3";
  
MagicParser_parse($country_url,"myCountryHandler","xml|CTRY_LIST/COUNTRY/");
  
//debug
  //print_r($array);
// Parse the countries end
// Parse the regions start
  
function myRegionHandler($region)
  {
      global 
$country_code;
      global 
$country_name;
      print 
"".$country_code."<br>";
      print 
"".$country_name."<br>";
      print 
"".$region["REGION_CODE"]."<br>";
      
//print "".$region["REGION_COUNTRY"]."<br>";
      
print "".$region["REGION_DESCRIPTION"]."<br><br>";
  }
  
$region_url "http://xml.laterooms.com/xml_region.php3?ctry=".$country_code."";
  
MagicParser_parse($region_url,"myRegionHandler","xml|REGION_LIST/REGION/");
// Parse the regions end
?>

Thanks,
Simon

Submitted by support on Fri, 2006-08-25 12:24

Hi Simon,

Did you catch my second post above with the new code to put the results into an array....!

Cheers,
David.

Submitted by crounauer on Fri, 2006-08-25 12:35

Hi David,

Yes - Thanks!

I think we have misunderstood each other - sorry!

Each country has a seperate list of regions(counties). The first url (http://xml.laterooms.com/xml_ctry.php3) only displays the country code and country name.

The second url (http://xml.laterooms.com/xml_region.php3?ctry=1) displays the region information (region_name, region_code and region_country) depending on which country code was selected.

So the results I would expect to see would be a list of the regions from each country.

Hope this makes more sense.

Simon.

Submitted by support on Fri, 2006-08-25 12:41

I understand....

Bear in mind this is going to result in requesting a large number of feeds every page view - I presume you would be planning to load these into a database?

Anyway, have a go with this:

<?php
  
// Create a global array to hold countries indexed by code
  
$countries = array();
  
// Parse the countries start
  
function myCountryHandler($country)
  {
      global 
$countries;
      
$countries[$country["COUNTRY_CODE"]] = $country["COUNTRY_NAME"];
  }
  
$country_url "http://xml.laterooms.com/xml_ctry.php3";
  
MagicParser_parse($country_url,"myCountryHandler","xml|CTRY_LIST/COUNTRY/");
  
// Parse the countries end
  // Parse the regions start
  
function myRegionHandler($region)
  {
      global 
$countries;
      print 
"".$region["REGION_CODE"]."<br>";
      print 
"".$region["REGION_COUNTRY"]."<br>";
      print 
"".$countries[$region["REGION_COUNTRY"]]."<br>";
      print 
"".$region["REGION_DESCRIPTION"]."<br><br>";
  }
  foreach(
$countries as $key => $value)
  {
    
$region_url "http://xml.laterooms.com/xml_region.php3?ctry=".$key."&lang=eng";
    
MagicParser_parse($region_url,"myRegionHandler","xml|REGION_LIST/REGION/");
  }
  
// Parse the regions end
?>

Notice the foreach loop. That will make the script call the regions URL for every country (!!)....(where $key is the country code)...

Cheers,
David.

Submitted by crounauer on Fri, 2006-08-25 12:43

Yes, they will be downloaded every morning into a database. I did set up a purely xml site, but was very slow. I did utelise cache which helped, but made a decision to use a database. - much quicker and more versatile!

Ill give the script a go - thanks for your help.

Regards,
Simon

Submitted by crounauer on Fri, 2006-08-25 14:15

Hi David,

Is it the same idea again to then use the "$key_country_code" and "$region_code" to parse a third url for specific hotel info?

<?php
http
://xml.laterooms.com/xml_1.php3?pid=1453&ctry=".$key_country_code."&region=".$region_code."";
?>

<?php
  
// Create a global array to hold countries indexed by code
  
$countries = array();
  
// Parse the countries start
  
function myCountryHandler($country)
  {
      global 
$countries;
      
$countries[$country["COUNTRY_CODE"]] = $country["COUNTRY_NAME"];
      
//print "".$country["COUNTRY_CODE"]."<br>";
      //print "".$country["COUNTRY_NAME"]."<br><br>";
  
}
  
$country_url "http://xml.laterooms.com/xml_ctry.php3";
  
MagicParser_parse($country_url,"myCountryHandler","xml|CTRY_LIST/COUNTRY/");
  
// Parse the countries end
  // Create a global array to hold regions indexed by code
  
$regions = array();
  
// Parse the regions start
  
function myRegionHandler($region)
  {
      global 
$countries;
      global 
$regions;
      global 
$key_country_code;
      global 
$region_code;
      
$regions[$region["REGION_CODE"]] = $country["COUNTRY_CODE"];
      
$region_code $region["REGION_CODE"];
      print 
"".$key_country_code."<br>";
      print 
"".$region["REGION_COUNTRY"]."<br>";
      print 
"".$region["REGION_CODE"]."<br>";
      print 
"".$region["REGION_DESCRIPTION"]."<br><br>";
  }
  foreach(
$countries as $key_country_code => $value)
  {
    
$region_url "http://xml.laterooms.com/xml_region.php3?ctry=".$key_country_code."";
    
MagicParser_parse($region_url,"myRegionHandler","xml|REGION_LIST/REGION/");
  }
  
// Parse the regions end
  
print_r($regions);
  
// Parse the hotels start
  
function myHotelHandler($hotel)
  {
      global 
$countries;
      global 
$key_country_code;
      print 
"".$hotel["HOTEL_NAME"]."<br>";
      print 
"".$hotel["HOTEL_ADDRESS"]."<br>";
      print 
"".$hotel["HOTEL_PCODE"]."<br><br>";
  }
     foreach(
$regions as $key_country_code => $value)
     { 
    
$hotel_url "http://xml.laterooms.com/xml_1.php3?pid=1453&ctry=".$key_country_code."&region=".$region_code."";
    
MagicParser_parse($hotel_url,"myHotelHandler","xml|REGION_LIST/REGION/");
  }
  
// Parse the hotels end
?>

Submitted by support on Fri, 2006-08-25 14:18

That's correct - although I notice you still have the REGION_LIST format string in your last call to MagicParser_parse() - I guess that should be the correct format string for the hotel feed...

Cheers,
David.

Submitted by crounauer on Fri, 2006-08-25 14:31

Ok, when creating the last array it isn't picking up the country code?

Have I forgotten to global something?

the debug is

Array ( [248] => [247] => [251] => etc...

<?php
  
// Create a global array to hold countries indexed by code
  
$countries = array();
  
// Parse the countries start
  
function myCountryHandler($country)
  {
      global 
$countries;
      
$countries[$country["COUNTRY_CODE"]] = $country["COUNTRY_NAME"];
      
//print "".$country["COUNTRY_CODE"]."<br>";
      //print "".$country["COUNTRY_NAME"]."<br><br>";
  
}
  
$country_url "http://xml.laterooms.com/xml_ctry.php3";
  
MagicParser_parse($country_url,"myCountryHandler","xml|CTRY_LIST/COUNTRY/");
  
// Parse the countries end
  // Create a global array to hold regions indexed by code
  
$regions = array();
  
// Parse the regions start
  
function myRegionHandler($region)
  {
      global 
$countries;
      global 
$regions;
      global 
$key_country_code;
      global 
$region_code;
      global 
$country_code;
      
$regions[$region["REGION_CODE"]] = $country["COUNTRY_CODE"];
      
$region_code $region["REGION_CODE"];
      
$country_code $country["COUNTRY_CODE"];
      print 
"".$key_country_code."<br>";
      print 
"".$region["REGION_COUNTRY"]."<br>";
      print 
"".$region["REGION_CODE"]."<br>";
      print 
"".$region["REGION_DESCRIPTION"]."<br><br>";
  }
  foreach(
$countries as $key_country_code => $value)
  {
    
$region_url "http://xml.laterooms.com/xml_region.php3?ctry=".$key_country_code."";
    
MagicParser_parse($region_url,"myRegionHandler","xml|REGION_LIST/REGION/");
  }
  
// Parse the regions end
  
print_r($regions);
  
// Parse the hotels start
  
function myHotelHandler($hotel)
  {
      global 
$countries;
      global 
$key_country_code;
      global 
$region_code;
      global 
$country_code;
      print 
"".$hotel["IMAGES/URL"]."<br>";
      print 
"".$hotel["HOTEL_REF"]."<br>";
      print 
"".$hotel["HOTEL_STAR"]."<br>";
      print 
"".$hotel["HOTEL_ADDRESS"]."<br>";
      print 
"".$hotel["HOTEL_CITY"]."<br>";
      print 
"".$hotel["HOTEL_PCODE"]."<br>";
      print 
"".$hotel["HOTEL_REF"]."<br>";
      print 
"".$hotel["HOTEL_DESCRIPTION"]."<br><br>";
  }
     foreach(
$regions as $key_country_code => $value)
     {
    
$hotel_url "http://xml.laterooms.com/xml_1.php3?pid=1453&ctry=".$key_country_code."&region=".$region_code."";
    
MagicParser_parse($hotel_url,"myHotelHandler","xml|HOTEL_SEARCH/HOTEL/");
    }
  
// Parse the hotels end
?>

Submitted by support on Fri, 2006-08-25 15:10

Hi Simon,

Within your myRegionHandler, you're assigning $regions[$region["REGION_CODE"]] to $country["COUNTRY_CODE"]; however $country is not a local variable within myRegionHandler (it's part of the country handler function).

I'm not quite sure what region code should be mapped to - I presume it's region description? If so, the code should be as follows:

<?php
  
function myRegionHandler($region)
  {
      global 
$countries;
      global 
$regions;
      global 
$key_country_code;
      global 
$region_code;
      global 
$country_code;
      
$regions[$region["REGION_CODE"]] = $region["REGION_DESCRIPTION"];
      
$region_code $region["REGION_CODE"];
      
$country_code $country["COUNTRY_CODE"];
      print 
"".$key_country_code."<br>";
      print 
"".$region["REGION_COUNTRY"]."<br>";
      print 
"".$region["REGION_CODE"]."<br>";
      print 
"".$region["REGION_DESCRIPTION"]."<br><br>";
  }
?>

It might help to remove a lot of the unused global variables from earlier attempts - that will make it easier to see what's going on...

I can see what you need to do - i'll try and break it down and post some code in a couple of minutes...

Cheers!
David.

Submitted by crounauer on Fri, 2006-08-25 15:17

Thank you David.

Submitted by support on Fri, 2006-08-25 15:19

Hi Simon,

See if this helps - i've commented each step...

<?php
  
// create a global array to hold countries indexed by code
  
$countries = array();
  
// create a global array to hold regions, indexed by country
  
$regions = array();
  
// Parse the countries start
  
function myCountryHandler($country)
  {
    global 
$countries;
    
$countries[$country["COUNTRY_CODE"]] = $country["COUNTRY_NAME"];
  }
  
$country_url "http://xml.laterooms.com/xml_ctry.php3";
  
MagicParser_parse($country_url,"myCountryHandler","xml|CTRY_LIST/COUNTRY/");
  
// Parse the countries end
  // Parse the regions start
  
function myRegionHandler($region)
  {
    global 
$regions;
    
$regions[$region["REGION_COUNTRY"]][$region["REGION_CODE"]] = $region["REGION_DESCRIPTION"];
  }
  foreach(
$countries as $key => $value)
  {
    
$region_url "http://xml.laterooms.com/xml_region.php3?ctry=".$key."";
    
MagicParser_parse($region_url,"myRegionHandler","xml|REGION_LIST/REGION/");
  }
  
// Parse the regions end
  // Parse the hotels start
  
function myHotelHandler($hotel)
  {
      print 
"".$hotel["IMAGES/URL"]."<br>";
      print 
"".$hotel["HOTEL_REF"]."<br>";
      print 
"".$hotel["HOTEL_STAR"]."<br>";
      print 
"".$hotel["HOTEL_ADDRESS"]."<br>";
      print 
"".$hotel["HOTEL_CITY"]."<br>";
      print 
"".$hotel["HOTEL_PCODE"]."<br>";
      print 
"".$hotel["HOTEL_REF"]."<br>";
      print 
"".$hotel["HOTEL_DESCRIPTION"]."<br><br>";
  }
  
// need to get hotels for each region of each country, so this needs nested foreach loops
  
foreach($regions as $country_code => $country)
  {
    foreach(
$country as $region_code => $region)
    {
      
$hotel_url "http://xml.laterooms.com/xml_1.php3?pid=1453&ctry=".$country_code."&region=".$region_code."";
      
MagicParser_parse($hotel_url,"myHotelHandler","xml|HOTEL_SEARCH/HOTEL/");
    }
  }
  
// Parse the hotels end
?>

Cheers!
David.

Submitted by crounauer on Fri, 2006-08-25 15:20

The variable that are needed to parse the the last url are the region_code and country_code which is why I thought they needed to be mapped.

Submitted by support on Fri, 2006-08-25 15:25

It gets tricky because you need to create a 3 dimensional associative array. In my last code above (did that work by the way - but be carefull - it could end up making hundreds of requests to laterooms!) you'll see that $countries is only used as far as the region handler. After that all the information you need to create a hotel link is in the $regions array; which looks like this.

$regions[country-code][region-code] = [region-description];

That's where the 2 nested foreach loops - the outer foreach loops through each country-code, and the inner foreach loops through each region-code for that country...!

Hope this helps!
Cheers,
David.

Submitted by crounauer on Thu, 2006-08-31 10:14

Hi David,

Thanks for your help so far.

I have not tried it yet as I am very aware that it would make thousands of requests to laterooms and don't want to be banned from their program for all the wrong reasons.

Before I do try it, I thought that I should at least time the requests so that it does not totally overwhelm their sysytem.

Do you think it would be best to implement a counter for the final parser and then use an "if" statement to pause for a couple of seconds before continuing?

Regards,
Simon.

Submitted by support on Thu, 2006-08-31 10:17

Hi Simon,

Yes - that's sensible. You can use the sleep() function to pause execution rather than a big loop, for example:

<?php
  
foreach($regions as $country_code => $country)
  {
    foreach(
$country as $region_code => $region)
    {
      
$hotel_url "http://xml.laterooms.com/xml_1.php3?pid=1453&ctry=".$country_code."&region=".$region_code."";
      
MagicParser_parse($hotel_url,"myHotelHandler","xml|HOTEL_SEARCH/HOTEL/");
      
sleep(2);
    }
  }
?>

Cheers,
David.

Submitted by crounauer on Thu, 2006-08-31 12:03

Hi David,

I need to do a debug...

Am I correct in saying that if I limit the number of items parsed with this code it should allow me to debug and al least see some results?

<?php
  $counter 
0;
  function 
myHotelHandler($hotel)
  {
      global 
$counter;
      
$counter++;
      print 
"".$hotel["IMAGES/URL"]."<br>";
      print 
"".$hotel["HOTEL_REF"]."<br>";
      print 
"".$hotel["HOTEL_STAR"]."<br>";
      print 
"".$hotel["HOTEL_ADDRESS"]."<br>";
      print 
"".$hotel["HOTEL_CITY"]."<br>";
      print 
"".$hotel["HOTEL_PCODE"]."<br>";
      print 
"".$hotel["HOTEL_REF"]."<br>";
      print 
"".$hotel["HOTEL_DESCRIPTION"]."<br><br>";
      return (
$counter == 5);
  }
?>