2012-04-30 4 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 테이블에 표시 할 것입니다. 그러나 나는 내가 후자를 관리 할 수 ​​있다고 생각한다. 코드 예제를 도와 주시겠습니까? 제발 & 고마워요 !!배열을 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

루트 노드가 하나만있을 수 있기 때문에 여기에 모든 노드가 루트에 추가됩니다. –

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