简体   繁体   中英

PHP: SimpleXMLElement in while loop iterates only once

I'm trying to retrieve multiple .xml files (001.xml, 002.xml, 002.xml, etc), add new child information from a database table, then save the modified information as a new file locally.

Even though I have many rows in the database, using the SimpleXMLElement function prevents the script from executing more than once.

Here's a simplified version of the code:

$query=mysql_query('SELECT id FROM `table`');

while($row = mysql_fetch_array($query))
{    
    $contents = file_get_contents('path_to_url'.$row[0]);
    $xml = new SimpleXMLElement($contents);

    // Append information to xml data
    for($i=0; $i < count($xml->parent->children->child); $i++)
    {             
        $query=mysql_query('
            SELECT `age`
            FROM `table2`
            WHERE `name` = '.$xml->parent->children->child[$i]->name
        );
        $childage = mysql_fetch_array($query);

        $xml->parent->children->child[$i]->addChild('age',$childage[0]);
    }

    $file = 'id'.$row[0].'.xml'; 

    file_put_contents($file, $xml->asXML());      
}

Here's the original sample .xml data:

001.xml    
<parent>
    <children>
        <child>
            <name>herp</name>
        </child>
        <child>
            <name>derp</name>
        </child>          
    </children>
<parent>

002.xml    
<parent>
    <children>
        <child>
            <name>manny</name>
        </child>
        <child>
            <name>joe</name>
        </child>
        <child>
            <name>jack</name>
        </child>            
    </children
</parent>

This would be the output (note that 002.xml is not written because the loop stops)

001.xml    
<parent>
    <children>
        <child>
            <name>herp</name>
            <age>10</age>
        </child>
        <child>
            <name>derp</name>
            <age>12</age>
        </child>          
    </children>
</parent>

Change

while($row = mysql_fetch_array($query, MYSQL_NUM)) 

to

while ($row=mysql_fetch_array($query))

line for($i=0; $i < count($xml->parent1->child1); $i++) needs to be changed to for($i=1; $i <= mysql_num_rows($query); $i++)

$query=mysql_query('SELECT id FROM `table`', MYSQL_NUM);

while($row = mysql_fetch_array($query))
{    
$xml = new SimpleXMLElement('path_to_url'.$row[0].'.xml')) 

// Append information to xml data
for($i=1; $i <= mysql_num_rows($query); $i++)
{             
    $query=mysql_query('
        SELECT `id`
        FROM `table2`
        WHERE `column` = '.$xml->parent->child1[$i]
    );
    $row = mysql_fetch_assoc($query, MYSQL_NUM);

    $xml->parent->child1[$i]->addChild('child2',$row[0]);
}

$file = 'id'.$row[0].'.xml'; 

file_put_contents($file, $xml);      
}

Try put your code inside a function first, then call the function from within a loop (outside that scope). That's the only way I was able to call SimpleXMLElement more than once within a loop.

Example:

while($row = mysql_fetch_array($query))
{  
    myFunction($row);
}  

function myFunction($row){
    $xml = new SimpleXMLElement('path_to_url'.$row[0].'.xml')) 

    // Append information to xml data
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM