0
我有一個正在進行的XML文件,當我調用一個PHP函數來添加一個新的孩子,它循環通過一個字符串數組,並查詢一個數據庫添加一個新的孩子該文檔並將其保存爲數組中的當前字符串。然而,它不是追加,它覆蓋了一切。我需要先加載文件嗎?並檢查它是否存在?PHP simpleXML,而不是追加,寫入所有內容
function createUnitsXML($units,$wcccanumber,$mysqli) {
// Delete whitespaces and create an array of units assigned to call
$unit = preg_replace('/\s+/', '', $units);
$unitsarray = explode(",",$unit);
for ($i = 0; $i < count($unitsarray); $i++) {
$xml = new SimpleXMLElement('<xml/>');
$query = "SELECT * FROM calls WHERE wcccanumber = '$wcccanumber'";
$result = $mysqli->query($query);
while($row = mysqli_fetch_assoc($result)) {
$draw = $xml->addChild('call');
$draw->addChild('wcccanumber',$row['wcccanumber']);
$draw->addChild('currentcall',$row['call']);
$draw->addChild('county',$row['county']);
$draw->addChild('id',$row['id']);
$draw->addChild('location',$row['location']);
$draw->addChild('callcreated',$row['callcreated']);
$draw->addChild('station',$row['station']);
$draw->addChild('units',$row['units']);
$draw->addChild('calltype',$row['calltype']);
$draw->addChild('lat',$row['lat']);
$draw->addChild('lng',$row['lng']);
$draw->addChild('inputtime',$row['inputtime']);
}
$fp = fopen("xml/units/$unitsarray[$i].xml","wb");
fwrite($fp,$xml->asXML());
fclose($fp);
}
echo "--- Created units XML document for call: $wcccanumber";
echo "</br>";
}
非常感謝,工作! –
非常好,我很高興這個作品! –
這將創建無效的XML文件。請注意。 – hakre