You are here:  » Using a function to trigger MagicParser_parse


Using a function to trigger MagicParser_parse

Submitted by yorkepb on Sat, 2007-05-05 15:36 in

Hi,

I am fairly new to this application, just purchased it a few days ago, so if this is a question you already answered please bear with me.

I am trying to use a function to trigger MagicParser, and actually the idea behind this is, I am using MagicParser to get a whole years worth of calendar data each time the page is hit. So what I intend to do, is to get it the first time and store it in a my own format locally. Then in the future all I would need to do is to compare the two updated dates to determine if I need to download the information again.

so it would look something like this:

function getLastUpdated($item) {
  $lastUpdate = $item['UPDATED'];
  // Here we can compare the updated date with the one we
  // have stored locally and if they do not match then we
  // call this function:
  if(...) {
    MagicParser_parse($url,"updateCal","xml|feed/");
  }
}
function updateCal($item) {
  // Here we take the data and format it in my own way
  // for local storage
}
// This calls the initial function for checking the date
MagicParser_parse($url,"getLastUpdated","xml|feed/updated/");

unfortunately what is happening, the function call to MagicParser_parse which is located within the function (which is called by the original MagicParser) never gets called, never runs. I have tried this without any if statements proceeding it and it is still not being called. I have also tried using $this->updateCal for the second parameter to no avail.

Can you tell me why this may not be working and if you know of any issues regarding running this second MagicParser from within a function.

BTW, I have moved it out of the function so it is just called when the page loads and it works that way along with the other call to it. It is only inside the function that I run into these problems. Of course this becomes poses a big problem to what I am trying to do, as most of the time (for this project) that MagicParser will be called is from within a function.

Submitted by support on Sun, 2007-05-06 09:08

Hi,

Because MagicParser_parse() is not an object oriented module you won't be able to call it whilst still under control of the previous parsing instance. This is because all the global variables used by MagicParser would be overwritten.

However, looking at your code, an alternative that would avoid nesting calls to the parser is to set a global variable on the condition that you are testing in the first parse, and then when that parse has finished you can look at the variable to decide whether or not to do the second parse. This is what I mean:

$updated = FALSE;
function getLastUpdated($item) {
  global $updated;
  $lastUpdate = $item['UPDATED'];
  // Here we can compare the updated date with the one we
  // have stored locally and if they do not match then we
  // set $updated to be picked up outside this function..
  if(...) {
    $updated = TRUE;
  }
}
function updateCal($item) {
  // Here we take the data and format it in my own way
  // for local storage
}
// This calls the initial function for checking the date
MagicParser_parse($url,"getLastUpdated","xml|feed/updated/");
// Now look to see if the $updated flag is set, and if so
// we call the second parse
if ($updated)
{
  MagicParser_parse($url,"updateCal","xml|feed/");
}

Hope this helps!
Cheers,
David.