You are here:  » keep variables in looping through and out of function


keep variables in looping through and out of function

Submitted by RonE on Mon, 2013-05-20 01:27 in

Hi David

I have the following code which gets the codes for each room of the fours rooms chosen.It returns 4 arrays (one for each room)
The returned results are all correct but I am hoping to have all the codes for each of the four rooms available in each room and out of the recordhandler function that I can use variables in a link to the next page.

Is this possible
thanks in advance
Ron

<?php
require("MagicParser.php");
MagicParser_parse("{link saved}","myRecordHandler","xml|HOTELSHOPPINGRESPONSE/HOTELS/HOTEL/RATES/");
$codearray = array();
$codearray2 = array();
$codearray3 = array();
$codearray4 = array();
function 
myRecordHandler($result)
  { 
// edited code to your response
 
global $codearray1;
 global 
$codearray2;
 global 
$codearray3;
 global 
$codearray4;
// loop and display all
    
$child = array();
    
$rates = array();
    
$rate = array();
    
$roomdata = array();
    
$roomtype = array();
    
$roomdesc = array();
    
$ratecode = array();
    
$roomcode = array();
    
$rateaccesscode = array();
    
$currentRoom "";
    foreach(
$result as $key => $value)
    {
        if ( (
strpos($key,"RATES")!==FALSE) &&(strpos($key,"ROOM")!==FALSE) )
    {
    
$currentRoom $value;
    
$child[$currentRoom] = array();
    
$rates[$currentRoom] = array();
    
$rate[$currentRoom] = array();
    
$roomdata[$currentRoom] = array();
    
$roomtype[$currentRoom] = array();
    
$roomdesc[$currentRoom] = array();
    
$ratecode[$currentRoom] = array();
    
$roomcode[$currentRoom] = array();
    
$rateaccesscode[$currentRoom] = array();
    continue;
  }
    if (
strpos($key,"ROOMDESCRIPTIONDATA")!==FALSE)
  {
    
$roomdata[$currentRoom][] = $value;
    continue;
  }
  if ( (
strpos($key,"ROOMTYPE")!==FALSE) && (strpos($key,"CODE")!==FALSE) )
  {
    
$roomtype[$currentRoom][] = $value;
    continue;
  }
        if (
strpos($key,"ROOMDESCRIPTION")!==FALSE)
    {
    
$roomdesc[$currentRoom][] = $value;
    continue;
    }
        if (
strpos($key,"ROOMCODE")!==FALSE)
            {
            
$roomcode[$currentRoom][] = $value;
             continue;
             }
        if (
strpos($key,"RATECODE")!==FALSE)
            {
            
$ratecode[$currentRoom][] = $value;
              continue;
             }
        if (
strpos($key,"RATEACCESSCODE")!==FALSE)
            {
            
$rateaccesscode[$currentRoom][] = $value;
              continue;
             }
    }
// printing results
echo "\t<br><b>Room: " $currentRoom[$z] . "</b><br><br>\n";
// removed global $codearray1, $codearray2, $codearray3, $codearray4;
$codearray1 = array($roomcode[1][0], $ratecode[1][0], $rateaccesscode[1][0]);
$codearray2 = array($roomcode[2][0], $ratecode[2][0], $rateaccesscode[2][0]);
$codearray3 = array($roomcode[3][0], $ratecode[3][0], $rateaccesscode[3][0]);
$codearray4 = array($roomcode[4][0], $ratecode[4][0], $rateaccesscode[4][0]);  
echo 
"\troom 1 codes\n";
print_r($codearray1);
echo 
"\t<br><br>room 2 codes\n";
print_r($codearray2);
echo 
"\t<br><br>room 3 codes\n";
print_r($codearray3);
echo 
"\t<br><br>room 4 codes\n";
print_r($codearray4);
echo 
"\t<br><br>\n";
    }
echo 
"\t<br><br><b>show each rooms code out of function</b>\n";
echo 
"\t<br><br>room 1 codes\n";
print_r($codearray1);
echo 
"\t<br><br>room 2 codes\n";
print_r($codearray2);
echo 
"\t<br><br>room 3 codes\n";
print_r($codearray3);
echo 
"\t<br><br>room 4 codes\n";
print_r($codearray4); 
?>

resulting webpage

Room: 1

room 1 codes Array ( [0] => 435568351 [1] => NR2 [2] => 648216661 )

room 2 codes Array ( [0] => [1] => [2] => )

room 3 codes Array ( [0] => [1] => [2] => )

room 4 codes Array ( [0] => [1] => [2] => )

Room: 2

room 1 codes Array ( [0] => [1] => [2] => )

room 2 codes Array ( [0] => 435568351 [1] => NR2 [2] => 648216661 )

room 3 codes Array ( [0] => [1] => [2] => )

room 4 codes Array ( [0] => [1] => [2] => )

Room: 3

room 1 codes Array ( [0] => [1] => [2] => )

room 2 codes Array ( [0] => [1] => [2] => )

room 3 codes Array ( [0] => 435568251 [1] => NR2 [2] => 648216661 )

room 4 codes Array ( [0] => [1] => [2] => )

Room: 4

room 1 codes Array ( [0] => [1] => [2] => )

room 2 codes Array ( [0] => [1] => [2] => )

room 3 codes Array ( [0] => [1] => [2] => )

room 4 codes Array ( [0] => 435568291 [1] => NR2 [2] => 648216661 )

show each rooms code out of function

room 1 codes Array ( [0] => [1] => [2] => )

room 2 codes Array ( )

room 3 codes Array ( )

room 4 codes Array ( )

Submitted by support on Mon, 2013-05-20 08:18

Hello Ron,

Sure - all you need to do is to declare your codearrayN variables as global at the top of your myRecordHandler function, that's all... e.g.

  function myRecordHandler($result)
  {
    global $codearray1;
    global $codearray2;
    global $codearray3;
    global $codearray4;
    // ... rest of code ...

Cheers,
David
--
MagicParser.com

Submitted by support on Mon, 2013-05-20 10:12

Hello Ron,

Please could you let me know the source URL, my apologies I accidentally removed it before saving, I'll re-create your script and check it out on my test sever for you - it's probably best to create $codearray as an array itself...

Thanks,
David

Submitted by support on Mon, 2013-05-20 12:37

Hello Ron,

As your XML source contains nested XML elements (RATES) within each main repeating element (HOTEL) it's probably best to parse at the HOTELSHOPPINGRESPONSE/HOTELS/HOTEL/ level, and then use a loop to extract each RATES/RATE by making use of the way Magic Parser resolves duplicated elements. For example, the first room average nightly rate is:

$result["RATES/RATE/AVERAGENIGHTLYRATE"]

...and the second room:

$result["RATES/RATE/AVERAGENIGHTLYRATE@1"]

So you can use a loop to construct @n, test if that rate record exists and add to a $rates array, and then finally make that an element of a $hotel array, which itself is then an element of the global $hotels.

A great way to test / debug code such as this is to set your content type to text/plain, which makes the output of print_r() much more readable. Have a go with:

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($result)
  {
    global 
$hotels;
    
$hotel = array();
    
$hotel["HOTEL-NAME"] = $result["HOTEL-NAME"];
    
$rates = array();
    
$i 0;
    
$p "";
    while(
1)
    {
      if (
$i$p "@".$i;
      if (!isset(
$result["RATES".$p."-ROOM"])) break;
      
$room $result["RATES".$p."-ROOM"];
      
$rates[$room]["CHECKINDATE"] = $result["RATES/RATE".$p."-CHECKINDATE"];
      
$rates[$room]["CHECKOUTDATE"] = $result["RATES/RATE".$p."-CHECKOUTDATE"];
      
$rates[$room]["HOSTCODE"] = $result["RATES/RATE".$p."-HOSTCODE"];
      
$rates[$room]["HREF"] = $result["RATES/RATE".$p."-HREF"];
      
$rates[$room]["RATETYPE"] = $result["RATES/RATE".$p."-RATETYPE"];
      
$rates[$room]["AVERAGENIGHTLYRATE"] = $result["RATES/RATE/AVERAGENIGHTLYRATE".$p.""];
      
$rates[$room]["TOTALCOST"] = $result["RATES/RATE/TOTALCOST".$p.""];
      
$rates[$room]["TOTALCOST-CURRENCY"] = $result["RATES/RATE/TOTALCOST".$p."-CURRENCY"];
      
$rates[$room]["TOTALCOSTINCLUSIVE"] = $result["RATES/RATE/TOTALCOSTINCLUSIVE".$p.""];
      
$rates[$room]["NIGHTLYRATES"] = $result["RATES/RATE/NIGHTLYRATES".$p.""];
      
$rates[$room]["ROOMDESCRIPTIONDATA"] = $result["RATES/RATE/ROOMDESCRIPTIONDATA".$p.""];
      
$rates[$room]["MEALPLANINFOS"] = $result["RATES/RATE/MEALPLANINFOS".$p.""];
      
$rates[$room]["ADULT-COUNT"] = $result["RATES/OCCUPANTDETAILS/ADULT".$p."-COUNT"];
      
$rates[$room]["SENIOR-COUNT"] = $result["RATES/OCCUPANTDETAILS/SENIOR".$p."-COUNT"];
      
$i++;
    }
    
$hotel["RATES"] = $rates;
    
$hotels[] = $hotel;
  }
  
header("Content-Type: text/plain");
  
$hotels = array();
  
MagicParser_parse("{link saved}","myRecordHandler","xml|HOTELSHOPPINGRESPONSE/HOTELS/HOTEL/");
  
print_r($hotels);
?>

...replacing {link saved} with your source XML URL...

Hope this helps!
Cheers,
David
--
MagicParser.com

Submitted by RonE on Mon, 2013-05-20 23:56

Hi David

Thanks for your efforts.

The code you have created above is great but the problem is that there is several rates for each room and because of the $p variable it is grabbing the first four rates for room 1. not the first rate for each room 1-4. for example For each room there are several different rates you can choose with the first room returned as the selected room For example there are 12 rates for room 1.

When we parse

MagicParser_parse("{link saved}","myRecordHandler","xml|HOTELSHOPPINGRESPONSE/HOTELS/HOTEL/");

we want to returns these values

room1 seleccted
["RATE/AVERAGENIGHTLYRATE"]=>
string(6) "210.00"
room2 selected
["RATES/RATE/AVERAGENIGHTLYRATE@12"]=>
" string(6) "210.00"
room3 selected
["RATES/RATE/AVERAGENIGHTLYRATE@24"]=>
string(6) "274.00
room4 selected
["RATES/RATE/AVERAGENIGHTLYRATE@34"]=>
string(6) "365.00"

but with the code above we are pulling
["RATE/AVERAGENIGHTLYRATE"]=>
["RATES/RATE/AVERAGENIGHTLYRATE@1"]=>
["RATES/RATE/AVERAGENIGHTLYRATE@2"]=>
["RATES/RATE/AVERAGENIGHTLYRATE@3"]=>
which is the first four prices for room1

If we can get it to work parsing this way could you also add some code on how to loop within the loop the nested elements for example nightly rates.

when we parse to HOTEL/RATES/

it creates 4 arrays one for each room and the selected rooms are all ["RATE/AVERAGENIGHTLYRATE"]=> which is what we want but then it comes back to my original problem in my first post.

the link to the xml is {link saved}

Regards
Ron

Submitted by support on Tue, 2013-05-21 08:45

Hello Ron,

I'll follow up by email - I have an experimental version of Magic Parser that will let you parse at the HOTELSHOPPINGRESPONSE/HOTELS/HOTEL/RATES/RATE/ level, and for each rate (as $result) you will have access to all parent elements in the XML hierarchy.

I'll email that to you now with some example code to try...

Cheers,
David
--
MagicParser.com