Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Matcghing XML Data to database

    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 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

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    You wouldn't need nested loops. Try something like
    PHP Code:
    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
            
            
    }

            

    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,395
    Thanks
    13
    Thanked 353 Times in 349 Posts

    Macintosh

    first thing I recommend is moving the query out of the loop (you only need one query by the looks of it)

    PHP Code:
    // 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($resultMYSQL_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 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •