簡體   English   中英

PHP將數據從mysql數據庫輸出到XML文件

[英]PHP output data from mysql database to an XML file

我正在從mysql數據庫獲取數據到XML文件。 我正在使用以下PHP代碼來獲取輸出。

PHP代碼:

<?php

mysql_connect('localhost', 'root', '');
mysql_select_db('emptestdb');

$sql = "SELECT  name, username, DATE(visitdate) AS vdate, DATE(retdate) AS rdate, location FROM users";
$res = mysql_query($sql);

header("Content-Type: text/html/force-download");
header("Content-Disposition: attachment; filename='EMP_RECORDS.xml'");

$xml = new XMLWriter();

$xml->openURI("php://output");
$xml->startDocument('1.0','UTF-8');
$xml->setIndent(true);

$xml->startElement('engineers');

while ($row = mysql_fetch_assoc($res)) {
  $xml->startElement("engineer");

  $xml->writeAttribute('engID', $row['username']);
  $xml->writeAttribute('engName', $row['name']);

  $xml->startElement("location");
  $xml->writeAttribute('fieldLoc', $row['location']);

  $xml->writeElement('vistiDate',$row['vdate']);
  $xml->writeElement('retDate',$row['rdate']);

  $xml->endElement();
  $xml->endElement();
}

$xml->endElement();

header('Content-type: text/xml');

$xml->flush();
?>

使用上面的代碼時,我得到以下輸出XML輸出。

當前輸出:

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

    <engineers>
     <engineer engID="1" engName="John">
      <location fieldLoc="Geneva">
       <vistiDate>2015-01-23</vistiDate>
       <retDate>2015-06-28</retDate>
      </location>
     </engineer>
     <engineer engID="1" engName="John">
      <location fieldLoc="Paris">
       <vistiDate>2015-02-12</vistiDate>
       <retDate>2015-02-17</retDate>
      </location>
     </engineer>
     <engineer engID="2" engName="Josh">
      <location fieldLoc="Paris">
       <vistiDate>2015-02-12</vistiDate>
       <retDate>2015-02-17</retDate>
      </location>
     </engineer>
    <engineer engID="3" engName="Rita">
      <location fieldLoc="Paris">
       <vistiDate>2015-02-12</vistiDate>
       <retDate>2015-02-17</retDate>
      </location>
     </engineer>
    </engineers>

我正在嘗試對engID進行分組並獲得以下輸出

所需輸出:

<?xml version="1.0" encoding="UTF-8"?>
<engineers>
 <engineer engID="1" engName="John">
  <location fieldLoc="Geneva">
   <vistiDate>2015-01-23</vistiDate>
   <retDate>2015-06-28</retDate>
  </location>

  <location fieldLoc="Paris">
   <vistiDate>2015-02-12</vistiDate>
   <retDate>2015-02-17</retDate>
  </location>
</engineer>

 <engineer engID="2" engName="Josh">
  <location fieldLoc="Paris">
   <vistiDate>2015-02-12</vistiDate>
   <retDate>2015-02-17</retDate>
  </location>
 </engineer>

 <engineer engID="3" engName="Rita">
  <location fieldLoc="Paris">
   <vistiDate>2015-02-12</vistiDate>
   <retDate>2015-02-17</retDate>
  </location>
 </engineer>
</engineers>

有人可以指導我如何將其組合在一起。 謝謝。

一個不錯的主意應該是

  1. 處理您的數據並保存
  2. 生成您的xml

     $engineers = []; // process data while ($row = mysql_fetch_assoc($res)) { if (!isset($engineers[$row['username']])) { $engineers[$row['username']] = [ 'name' => $row['name'], 'locations' => [], ]; } $engineers[$row['username']]['locations'][] = [ $xml->writeAttribute('fieldLoc', $row['location']); 'vistiDate' => $row['vdate'], 'retDate' => $row['rdate']); ]; } // generate xml foreach ($engineers as $engineer) { // add engineer info to xml foreach ($engineer['locations'] as $location) { // add location to xml } } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM