You are here:  » Access Array Values Outside of myrecordhandler fuction


Access Array Values Outside of myrecordhandler fuction

Submitted by jsaydm on Wed, 2007-10-31 21:16 in

Hi I have the following code and I would like to echo out each title and ID of the video outside of the function. I know it can be done within the function but I'd like to keep all the HTML formatting out for ease of use and flexibility.

<?php
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
  global 
$youtube_title,$youtube_desc,$youtube_embed;
  
$youtube_title=$record["MEDIA:TITLE"];
  
$youtube_embed=$record["MEDIA:CONTENT-URL"];
  }
  
MagicParser_parse("http://gdata.youtube.com/feeds/videos?q=vw%20golf&max-results=5","myRecordHandler","xml|FEED/ENTRY/MEDIA:GROUP/");
$youtube_embed_array explode("http://www.youtube.com/v/"$youtube_embed);
$youtube_id=$youtube_embed_array[1];
echo 
$youtube_title "<br /><br />";
echo 
$youtube_id "<br /><br />";
?>

Submitted by support on Thu, 2007-11-01 08:23

Hi,

Generally, the only requirement for accessing variables outside of myRecordHandler is to make the variables that you copy the values into global, which you have done, so that indicates that this is not the problem.

Therefore, the first step is to add some debug code to find out why $youtube_title and $youtube_embed are not being set as expected. This will either be down to the fact that myRecordHandler is not being called at all, indicating that the actual parse itself is not working; or the parse is working but MEDIA:TITLE and MEDIA:CONTENT-URL are not valid fields within $record.

The easiest way to verify both of these cases is to display $record using the print_r() function, and check that a) something is displayed, and then b) the values you are using are within the array. I normally exit() the script after using print_r() so that no other output gets in the way of the debug output; so for example:

<?php
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    
print_r($record);exit();
    global 
$youtube_title,$youtube_desc,$youtube_embed;
    
$youtube_title=$record["MEDIA:TITLE"];
    
$youtube_embed=$record["MEDIA:CONTENT-URL"];
  }
  
MagicParser_parse("http://gdata.youtube.com/feeds/videos?q=vw%20golf&max-results=5","myRecordHandler","xml|FEED/ENTRY/MEDIA:GROUP/");
  
$youtube_embed_array explode("http://www.youtube.com/v/"$youtube_embed);
  
$youtube_id=$youtube_embed_array[1];
  echo 
$youtube_title "<br /><br />";
  echo 
$youtube_id "<br /><br />";
?>

Cheers,
David.