2016-06-27 19 views
0

xmlファイルに別の要素を追加しようとしています。 これは今作成したものです。ここでXMLを作成するPHPが正しく生成されないファイル

flipsnack> 
<title>Book 1</title> 
<date>6-6-2016</date> 
<link>google.com</link> 
<embed>this is hetml code</embed> 
<order>1</order> 
<show>1</show> 
<flipsnack> 
<title/> 
<link>hotmail.com</link> 
<embed>html code all the way</embed> 
<order>2</order> 
<postdate/> 
<show>1</show> 
</flipsnack> 
</flipsnack> 

が、私はそれがあるべきだと思うものです....ここで

<?xml version="1.0" encoding="UTF-8"?> 
<flipsnack> 

    <book> 
    <title>Book 1</title> 
    <date>6-6-2016</date> 
    <link>google.com</link> 
    <embed>this is hetml code</embed> 
    <order>1</order> 
    <show>1</show> 
    </book> 

    <book> 
    <title>Book 1</title> 
    <date>6-6-2016</date> 
    <link>google.com</link> 
    <embed>this is hetml code</embed> 
    <order>1</order> 
    <show>1</show> 
    </book> 
</flipsnack> 

は私のPHPコードです:

$title=$_POST["post"]; 
$date=$_POST["date"]; 
$link=$_POST["link"]; 
$html=$_POST["html"]; 
$order=$_POST["order"]; 
$show=$_POST["show"]; 

$xml = simplexml_load_file("db.xml"); 
$sxe = new SimpleXMLElement($xml->asXML()); 
$newItem = $sxe->addChild("flipsnack"); 
$newItem->addChild("title", $title); 
$newItem->addChild("link", $link); 
$newItem->addChild("embed", $html); 
$newItem->addChild("order", $order); 
$newItem->addChild("postdate", $postdate); 
$newItem->addChild("show", $show); 
$sxe->asXML("db.xml"); 

私は何をそのから取得する方法はかなりスタンドの下にいけません今私はそれをしたいと思っていますか?誰か私にある方向を教えてもらえますか?

+0

あなたのPHPを別にしてください – zanderwar

+0

@Zanderwar>更新 – Kelly

答えて

0

コードを見て、間違っていることを考えてください。

<?php 
$title = 'Book 1'; 
$link = 'google.com'; 
$order = 1; 
$postdate = 'some postdate'; 
$show = 'some show'; 
$sxe = new SimpleXMLElement ('<flipsnack/>'); 

for($i = 0; $i < 2; $i ++) { 
    $newItem = $sxe->addChild ("book"); 
    $newItem->addChild ("title", $title); 
    $newItem->addChild ("link", $link); 

    $newItem->addChild ("order", $order); 
    $newItem->addChild ("postdate", $postdate); 
    $newItem->addChild ("show", $show); 

} 
echo $sxe->asXML(); 
?> 

ここで出力

<?xml version="1.0"?> 
<flipsnack> 
    <book> 
     <title>Book 1</title> 
     <link>google.com</link> 
     <order>1</order> 
     <postdate>some postdate</postdate> 
     <show>some show</show> 
    </book> 
    <book> 
     <title>Book 1</title> 
     <link>google.com</link> 
     <order>1</order> 
     <postdate>some postdate</postdate> 
     <show>some show</show> 
    </book> 
</flipsnack> 
0

は私がやろうとしたものです。

$title=$_POST["title"]; 
$date=$_POST["date"]; 
$link=$_POST["link"]; 
$html=$_POST["html"]; 
$order=$_POST["order"]; 
$show=$_POST["show"]; 

$xml = simplexml_load_file("db.xml"); 
$sxe = new SimpleXMLElement($xml->asXML()); 
$newItem = $sxe->addChild("book"); 
$newItem->addChild("title", $title); 
$newItem->addChild("date", $date); 
$newItem->addChild("link", $link); 
$newItem->addChild("embed", $html); 
$newItem->addChild("order", $order); 
$newItem->addChild("show", $show); 
$sxe->asXML("db.xml"); 
関連する問題