2016-10-23 72 views
0

下面是我的XML表,我已經創建了PHP來添加一本書,但我正在努力如何更新nextID讓我說,我添加一本書在ID 101,但我如何更新所以NextID顯示102不是101.XML/PhP我如何更新節點NextID

<?xml version="1.0"?> 
<catalogue> 
    <nextID>101</nextID> 
    <tasks> 
    <task id="100"> 
     <title>Task Three</title> 
     <taskleader>Cara Forbes</taskleader> 
     <participants>Alex Lord</participants> 
     <targetdate>25/10/2016</targetdate> 
     <summary>Test Text Area</summary> 
     <status>completed</status> 
     <url>www.taskthree.com</url> 
    </task> 
    </tasks> 
</catalogue> 
+0

下面是我迄今爲止,它正確地添加任務。 –

回答

0

迴應以上。

我迄今whicj正確添加一個任務

$root= $xml->documentElement; 
    $nextIDNode=$root->childNodes->item(0); 
    $tasks= $root->childNodes->item(1); 

    $firstTask=$tasks->childNodes->item(0); 
    $newID=(int)$root->childNodes->item(0)->nodeValue; 
    $updateID = $newID + 1; 
    $newTitle=$_POST["addTitle"]; 
    $newTLead=$_POST["addLead"]; 
    $newPart=$_POST["addPart"]; 
    $newDate=$_POST["addDate"]; 
    $newSum=$_POST["addSum"]; 
    $newStat= "new"; 
    $newUrl=$_POST["addUrl"]; 


    $titleNode=$xml->createElement("title"); 
    $titleTextNode=$xml->createTextNode("$newTitle"); 
    $titleNode->appendChild($titleTextNode); 

    $taskLeaderNode=$xml->createElement("taskleader"); 
    $taskLeaderTextNode=$xml->createTextNode("$newTLead"); 
    $taskLeaderNode->appendChild($taskLeaderTextNode); 

    $taskPartNode=$xml->createElement("participants"); 
    $taskPartTextNode=$xml->createTextNode("$newPart"); 
    $taskPartNode->appendChild($taskPartTextNode); 
    $taskDateNode=$xml->createElement("targetdate"); 
    $taskDateTextNode=$xml->createTextNode("$newDate"); 
    $taskDateNode->appendChild($taskDateTextNode); 
    $taskSumNode=$xml->createElement("summary"); 
    $taskSumTextNode=$xml->createTextNode("$newSum"); 
    $taskSumNode->appendChild($taskSumTextNode);  
    $taskStatNode=$xml->createElement("status"); 
    $taskStatTextNode=$xml->createTextNode("$newStat"); 
    $taskStatNode->appendChild($taskStatTextNode); 
    $taskUrlNode=$xml->createElement("url"); 
    $taskUrlTextNode=$xml->createTextNode("$newUrl"); 
    $taskUrlNode->appendChild($taskUrlTextNode); 


    $newTaskNode=$xml->createElement("task"); 
    $newTaskNode->setAttribute("id",$newID); 
    $newTaskNode->appendChild($titleNode); 
    $newTaskNode->appendChild($taskLeaderNode); 
    $newTaskNode->appendChild($taskPartNode); 
    $newTaskNode->appendChild($taskDateNode); 
    $newTaskNode->appendChild($taskSumNode); 
    $newTaskNode->appendChild($taskStatNode); 
    $newTaskNode->appendChild($taskUrlNode); 
    $tasks->insertBefore($newTaskNode,$firstTask); 


    $xml->save("tasks.xml"); 


    $add = "Task added"; 
    echo $add; 
    $xml->save("tasks.xml");