How to use multiple conditions in foreach?

Discussion in 'PHP' started by max196, Jun 23, 2010.

  1. #1
    Hello Friends,
    I'm pulling data from an xml file and updating it to my mysql databse. The details are bellow.

    I'm puling data from info.xml file

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <dataxml>

    <data>
    <id>01</id>
    <name>Name1</name>
    </data>

    <data>
    <id>02</id>
    <name>Name2</name>
    </data>

    </dataxml>


    <?php
    // pull data from xml file

    $xml = new DOMDocument();
    $xml->load("info.xml");

    $details = $xml->getElementsByTagName("data");

    foreach( $details as $value )
    {
    $ids = $value->getElementsByTagName("id");
    $id_xml = $ids->item(0)->nodeValue;

    $names = $value->getElementsByTagName("name");
    $name_xml = $names->item(0)->nodeValue;

    // add information to database
    $query = "INSERT INTO detials (id, name) VALUES ('$id_xml', '$name_xml')";
    $result = mysql_query($query, $connection) or die(mysql_error());

    }
    ?>


    Its working fine but I want to validate the data if it already exists in DB. If it exists, the row will not be inserted.

    I used,

    $query = "SELECT * From details";
    $result = mysql_query($query, $connection) or die(mysql_error());

    while ($row = mysql_fetch_array($result))
    {
    $id = $row["id"];
    $name = $row["name"];

    if ($id != $id_xml){
    // add information to database
    $query = "INSERT INTO detials (id, name) VALUES ('$id_xml', '$name_xml')";
    $result = mysql_query($query, $connection) or die(mysql_error());
    } else{
    echo 'data already exists!';
    }
    }


    but didn't worked. I googled and found the answer to use multiple conditions in foreach but I was unable to do that. So could you help me fix the above problem?

    Thanx :)
     
    max196, Jun 23, 2010 IP
  2. Kaimi

    Kaimi Peon

    Messages:
    60
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try:
    
    <?php
    $xml = new DOMDocument();
    $xml->load("info.xml");
    $details = $xml->getElementsByTagName("data");
    
    foreach( $details as $value )
    {
    	$ids = $value->getElementsByTagName("id");
    	$id_xml = $ids->item(0)->nodeValue;
    
    	$names = $value->getElementsByTagName("name");
    	$name_xml = $names->item(0)->nodeValue;
    
    	$result = mysql_query("SELECT * FROM details WHERE id = '$id_xml'");
    	if(mysql_num_rows($result) == 0)
    	{
    		$query = "INSERT INTO details (id, name) VALUES ('$id_xml', '$name_xml')";
    		$result = mysql_query($query, $connection) or die(mysql_error());
    	}
    	else
    	{
    		echo 'data already exists';
    	}
    }
    ?>
    
    PHP:
     
    Kaimi, Jun 23, 2010 IP
  3. max196

    max196 Peon

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks Kaimi,
    It worked great. Thank you very much for your help. :)
     
    max196, Jun 23, 2010 IP
  4. georgiivanov

    georgiivanov Member

    Messages:
    62
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    25
    #4
    This way you will do 1/2 less queries against the database :)
     
    georgiivanov, Jun 23, 2010 IP