thecodedeveloper.com
thecodedeveloper.com

Reputation: 3240

Insert data into mysql php in diffrent rows after retrieve from xml file

test.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<propertyList>
  <business  type="food">
  <listingAgent id="1">
    <name>Spiro Abelas</name>
    <email>[email protected]</email>
  </listingAgent>
 </business>
 <business  type="food">
  <listingAgent id="2">
    <name>andy</name>
    <email>[email protected]</email>
  </listingAgent>
 </business>
</propertyList>

Php code Interprets an XML file into an object

<?php
  if (file_exists('test.xml')) {
    $xml = simplexml_load_file('test.xml');
      echo "<pre>";
      print_r($xml);
      echo "</pre>";
  } else {
     exit('Failed to open test.xml.');
     }
 ?>

Output

     SimpleXMLElement Object
(
[business] => Array
(
[0] => SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [type] => food
        )
    [listingAgent] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [id] => 1
                )
            [name] => spiro
            [email] => [email protected]
        )
)
[1] => SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [type] => food
        )
    [listingAgent] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [id] => 2
                )
            [name] => andy
            [email] => [email protected]
        )
)))

Table structure

enter image description here

i want to inset data like that into table

enter image description here

but i not getting how to retrieve data from array and insert into multiple rows

so plz guide me how to sort out it

Upvotes: 0

Views: 1285

Answers (1)

Bastian Rang
Bastian Rang

Reputation: 2187

foreach($xml->business as $row)
{
  $business_type = $row->attributes()->type;
  $id = $row->listingAgent->attributes()->id;
  $name = $row->listingAgent->name;
  $email = $row->listingAgent->email;
  mysql_query('INSERT INTO table…');
}

Upvotes: 1

Related Questions