DonQuery
DonQuery

Reputation: 47

'simplexml' php to mysql database loop return only first element

Be grateful for some help on this. I have a very long xml form with over 80 items to insert into my db.

I've researched this and for some reason my foreach loop doesn't work.

I've reduced the insert here to give an idea of what I am trying to do.

I can insert the first 'property / item into the database so I know I have no issues with the inserts etc.

For some reason the loop won't show the other 79 items in the db.

$affectedRow = 0;

$xml =  simplexml_load_file('properties2.xml') or die("can not find it");

foreach($xml->children()  as $row) {    
    $reference = $row->branch->properties->property['reference'];
    $instructedDate = $row->branch->properties->property->instructedDate;
    $price_text = $row->branch->properties->property->price_text;

    $sql = "INSERT INTO test( reference, instructedDate, price_text) VALUES ('". $reference ."','". $instructedDate ."','". $price_text ."')";

    $result = mysqli_query($conn, $sql);

    if (! empty($result )) {
        $affectedRow ++;
    } else {
        $error_message = mysqli_error($conn) . "\n";
    }
}
?>

eg xml file

-<agency branchName="billies Estate Agents " name="billie ea" xsi:noNamespaceSchemaLocation="http://www.feedcompany.co.uk/xmlexport/feedcompanyXMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    -<branches>

        -<branch name="billies Estate Agents ">

            -<properties>

                -<property reference="00000477">

                    <instructedDate>26/02/2018 15:11:56</instructedDate>

                    <price_text>Offers in Excess of £200,000</price_text>

                 </property

               -<property reference="00000478">

                    <instructedDate>26/02/2018 15:11:56</instructedDate>

                    <price_text>Offers in Excess of £200,000</price_text>

                </property>

Upvotes: 1

Views: 170

Answers (1)

dWinder
dWinder

Reputation: 11642

You only gets the first element into the DB because $xml->children() is not what you expected.

Notice that your XML starts with <agency> and after it you have <branches> tag. I guess your full XML is something like this:

<agency>
    <branches>
        <branch>
            <properties>
                <property>
                    ...
                </property>
                <property>
                    ...
                </property>
            </properties>
        </branch> 
    </branches>
</agency>

You want to get all the properties -> so you need to use $xml->branches->branch->properties->children().

Something like:

foreach($xml->branches->branch->properties->children() as $property) {
    $sql = "INSERT INTO test( reference, instructedDate, price_text) VALUES ('". $property['reference']."','". $property->instructedDate ."','". $property->price_text."')";
 ...
 } 

When you execute $xml->children() as in 3th line you gets the branches tag as the only element in the array - that why you have only one element insert to your DB.

Upvotes: 1

Related Questions