[英]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. 我正在尝试检索多个.xml文件(001.xml,002.xml,002.xml等),从数据库表中添加新的子信息,然后将修改后的信息另存为本地新文件。
Even though I have many rows in the database, using the SimpleXMLElement function prevents the script from executing more than once. 即使数据库中有很多行,使用SimpleXMLElement函数也可以防止脚本多次执行。
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: 这是原始示例.xml数据:
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) 这将是输出(请注意,由于循环停止,未写入002.xml)
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++)
需要将
for($i=0; $i < count($xml->parent1->child1); $i++)
行更改为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.
这是我能够在循环中多次调用SimpleXMLElement的唯一方法。
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
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.