2012-04-30 26 views
-1
//this is the code that creates the array and the output follows 
while ($results_row = mysql_fetch_assoc($results)) { 
     $returned_results[]= array(
            'partName'=>$results_row['partName'], 
            'description'=>$results_row['description'], 
            'price'=>$results_row['price'] 

//what is contained in the array is as follows 
Array 
(
[partName] => Cooler Master CM Storm Trooper Case 
[description] => Armored appearance. Nicely placed handle at the top. At the front; ext 
[price] => 36000 
) 
1 

Array 
(
[partName] => Cooler Master eXtreme Power Plus 500-Watt 
[description] => Power Plus 500-Watt Power Supply,many SATA and peripheral connectors. 
[price] => 23000 
) 
1 

Array 
(
[partName] => Coolmax M-500B ATX Power Supply 
[description] => The Coolmax M-500B ATX Power Supply has 5 SATA and 5 Peripheral, 8 pin 
[price] => 20000 
) 

配列変数($ returned_results [])に含まれるデータをxmlファイルに書き込む必要があります。その後、私はそれをxmlテーブルに表示します。しかし、私は後者を管理できると思う。コード例を教えてください。 Please &ありがとうございました!配列をXMLファイルに書き込む

xmlファイルには、この

<?xml version="1.0" encoding="utf-8"?> 
    <results> 
      <partName>Cooler Master CM Storm Trooper Case</partName> 
      <description>Armored appearance. Nicely placed handle at the top. At the front; ext... </description> 
      <price>36000</price> 
    </results> 
    <results> 
      <partName>Cooler Master eXtreme Power Plus 500-Watt</partName> 
      <description>Power Plus 500-Watt Power Supply,many SATA and peripheral connectors. ... </description> 
      <price>23000</price> 
    </results 
    <results> 
      <partName>Coolmax M-500B ATX Power Supply </partName> 
      <description>The Coolmax M-500B ATX Power Supply has 5 SATA and 5 Peripheral, 8 pin..</description> 
      <price>20000</price> 
    </results> 
+0

[スタックオーバーフローがあなたの個人的な研究助手ではありません](http://meta.stackexchange.com/a/128553/155197) – PeeHaa

答えて

0

あなたは自分のXMLを書くために(むしろechoまたはfwriteなどより)PHP自体を使用することができます。

ob_start(); 
?> 
<?xml version="1.0" encoding="utf-8"?> 
<?php 
while ($results_row = mysql_fetch_assoc($results)) { 
?> 
<results> 
    <partName><?php echo htmlspecialchars($results_row['partName']);?></partName> 
</results> 
<?php 
} 

file_put_contents('file.xml', ob_get_clean()); 
+0

O.K タグでエラーメッセージが表示されます。この文脈では、要素「結果」は子要素の本文として許可されていません。どうすればいいですか – Fadamie

+0

これはPHPの領域外の問題です:) –

+0

これは、ルートノードが1つしか存在しないためです。ここではすべてのノードにルートが追加されています()。 –

0

使用しますfwrite(ように見える配列データ)をconatin必要があります。例:

$filecontents = file_get_contents ("file.xml"); 
$file = fopen(file.xml, w+); 
//If you uncomment the next line, it will keep the current contents, if not it will overwrite it 
//fwrite($file, $filecontents); 
fwrite($file, $string/array-to-write); 
fclose($file); 
+0

私は明確ではなかった場合はごめんなさい。それは私が上で指定した特定のフォーマットのファイルに書き込むことに関するものです。 – Fadamie

+0

foreachループを使用しますか? –

+0

はい、私に例を教えてください。 – Fadamie

関連する問題