You are here:  » passing variable to function


passing variable to function

Submitted by mrtshaw on Wed, 2010-09-15 14:05 in

Hi there, I am trying to pass a variable to the magic parser function to insert into the db.

1st function will query a webservice to return the XML, then the magicparser will split the XML and insert into the DB acording to the advid.

getLocations("1");
function getLocations($advid)
{
    global $DB, $refreshpage, $sdurl;
//$ad = $DB->query_first("SELECT a_clientid FROM {p115_clients} WHERE advid = '".$advid."' ");
set_time_limit(0);
require_once('webservice/lib/nusoap.php');
require("webservice/MagicParser.php");
$client = new nusoap_client('http://URL/SimpleService?WSDL', true);
$err = $client->getError();
if ($err) {
echo '<h2>Constructor error</h2><pre>'.$err.'</pre>';
}
$data = array('advertiserId' => '2', 'onlyFirstLevel' => '0');
$result = $client->call('getAllLocationsFlatList', $data);
if ($client->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
} else {
$err = $client->getError();
if ($err) {
echo '<h2>Error</h2><pre>'.$err.'</pre>';
} else {
//echo $result['return'].'<br />';
MagicParser_parse("string://".$result['return'],"addLocations","xml|LOCATIONS/LOC/");
}
}
}
  function addLocations($record, $advid)
  {
    print $record["LOC"];
    print $record["ID"];
    print $record["PARENT"];
    print $record["SORT"];
    print $record["DISPLAYNAME"];
    print $record["SEARCHNAME"];
echo $advid
//insert into mysql DATA where advid = $advid
  }

Submitted by support on Wed, 2010-09-15 14:17

Hi,

I would start by putting $advid into a global variable, and then making sure that it is declared as global within your record handler function; for example:

$advid = "1";
getLocations($advid);
function getLocations($advid)
{
    global $DB, $refreshpage, $sdurl;
//$ad = $DB->query_first("SELECT a_clientid FROM {p115_clients} WHERE advid = '".$advid."' ");
set_time_limit(0);
require_once('webservice/lib/nusoap.php');
require("webservice/MagicParser.php");
$client = new nusoap_client('http://URL/SimpleService?WSDL', true);
$err = $client->getError();
if ($err) {
echo '<h2>Constructor error</h2><pre>'.$err.'</pre>';
}
$data = array('advertiserId' => '2', 'onlyFirstLevel' => '0');
$result = $client->call('getAllLocationsFlatList', $data);
if ($client->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
} else {
$err = $client->getError();
if ($err) {
echo '<h2>Error</h2><pre>'.$err.'</pre>';
} else {
//echo $result['return'].'<br />';
MagicParser_parse("string://".$result['return'],"addLocations","xml|LOCATIONS/LOC/");
}
}
}
  function addLocations($record, $advid)
  {
    global $advid;
    print $record["LOC"];
    print $record["ID"];
    print $record["PARENT"];
    print $record["SORT"];
    print $record["DISPLAYNAME"];
    print $record["SEARCHNAME"];
    echo $advid
    //insert into mysql DATA where advid = $advid
  }

Hope this helps!
Cheers,
David.

Submitted by mrtshaw on Wed, 2010-09-15 14:24

I can't really do that as there are numerous advertiser webservice feeds to parse.

function parseadvertisers()
{
mysql query(select * from advertiser)

while{
getLocations($advid);
}

}

Submitted by support on Wed, 2010-09-15 14:33

Hi,

That shouldn't be a problem - the same principle applies - basically any variable that you want to use within myRecordHandler needs to be global already, and then defined as global within myRecordHandler() - that's all.

If you're not sure and would like me to check out your full script please email me your code and I'll check it out for you...

Cheers,
David.