Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP simpleXML不是附加,而是覆盖所有内容_Php_Xml_Loops_Simplexml - Fatal编程技术网

PHP simpleXML不是附加,而是覆盖所有内容

PHP simpleXML不是附加,而是覆盖所有内容,php,xml,loops,simplexml,Php,Xml,Loops,Simplexml,我有一个正在进行的xml文件,当我调用php函数来添加一个新的子对象时,它会在字符串数组中循环,并查询数据库以向文档中添加一个新的子对象,并将其保存为数组中的当前字符串。但是,它不是附加,而是覆盖所有内容。我需要先加载文件吗?检查它是否存在 function createUnitsXML($units,$wcccanumber,$mysqli) { // Delete whitespaces and create an array of units assigned to call $unit

我有一个正在进行的xml文件,当我调用php函数来添加一个新的子对象时,它会在字符串数组中循环,并查询数据库以向文档中添加一个新的子对象,并将其保存为数组中的当前字符串。但是,它不是附加,而是覆盖所有内容。我需要先加载文件吗?检查它是否存在

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>";
}

通过以wb形式打开文件,您将截断要写入的文件。尝试使用ab只写,附加到文件末尾或ab+读或写,改为附加到文件末尾。

这将创建无效的XML文件。只是注意。是的,我注意到了,虽然它不会影响我的应用程序,但我该如何修复它?
$fp = fopen("xml/units/$unitsarray[$i].xml","wb");