...

View Full Version : Matcghing XML Data to database



graham23s
01-31-2010, 01:51 PM
Hi Guys,

What i'm trying to do is pull from and xml file and loop from the database, if 2 codes match then perform an action.

code:



<?php
include("inc/inc-dbconnection.php");

$call_url = 'http://www.site.com/load_xml.php?id=15320&subid=&geoip='.$user_ip;
$xml = simplexml_load_file($call_url);


//START Simple Output
foreach($xml->offer as $offers)
{
$c1 = $offers->country;
$q = mysql_query("SELECT * FROM `cpa_country_codes`");

while ($r = mysql_fetch_array($q))
{
$c2 = $r['code_country'];

if ($c1 == $c2)
{

//Perform action

}

}
}

?>


This is what i have concocted so far.

any help would be appreciated

thanks guys

Graham

abduraooft
01-31-2010, 03:15 PM
You wouldn't need nested loops. Try something like

foreach($xml->offer as $offers)
{
$c1 = $offers->country;
$q = mysql_query("SELECT code_country FROM `cpa_country_codes` where code_country= '$c1'");

if (mysql_num_rows($q ) )
{

//Perform action

}


}

Dormilich
01-31-2010, 03:23 PM
first thing I recommend is moving the query out of the loop (you only need one query by the looks of it)


// transform the DB results into an array
// no need to get all if you need one
$result = mysql_query("SELECT `code_country` FROM `cpa_country_codes`");
/* in PDO I’d call fetchAll() now … */
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
$db[] = $row[0];
}

// basicly do the same with SimpleXML
foreach ($xml->xpath('//country') as $sxe)
{
$xa[] = (string) $sxe;
}

// now for the fun part
$resultarray = array_intersect($db, $xa);

// et voilą, an array that holds the common values



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum