来自MySQL查询的多维数组以XML格式输出

时间:2021-10-19 02:11:54

My code:

$go = mysql_query($sql);
$fetch = mysql_fetch_array($go);
$return = Array($fetch[0],$fetch[1],$fetch[2]);  
$results = Array(  
    'body' => Array (  
           'entry' => Array ( 
            'title'    => $return[0],  
            'image'    => $return[1], 
            'caption'  => $return[2]
                )  
                   )
        ); 

XML Output:

This XML file does not appear to have any style information associated with it. The document tree is shown below.

此XML文件似乎没有与之关联的任何样式信息。文档树如下所示。

<callback xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema.xsd">
<body>
 <entry>
  <title>REHEARSAL DINNER IN SEATTLE, WA</title>
  <image>images/portfolio_entries/default.png</image>
  <caption>A wonderful in home event for 27 people.</caption>
 </entry>
</body>


I want it to loop through all of the rows from the query though and for the xml output to look something like this:

我希望它循环遍历查询中的所有行,而xml输出看起来像这样:

<callback xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema.xsd">
 <body>
  <entry>
   <title>REHEARSAL DINNER IN SEATTLE, WA</title>
   <image>images/portfolio_entries/default.png</image>
   <caption>A wonderful in home event for 27 people.</caption>
  </entry>
  <entry>
   <title>BLAH NUMBER TWO</title>
   <image>images/portfolio_entries/default2.png</image>
   <caption>Blah blah info about number two.</caption>
  </entry>
  <entry>
   <title>BLAH NUMBER THREE</title>
   <image>images/portfolio_entries/default3.png</image>
   <caption>blah blah info about number three.</caption>
  </entry>
 </body>
</callback>

1 个解决方案

#1


0  

echo '<callback xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema.xsd">
<body>'; 
foreach($results['entry'] as $data{
    echo '<entry>';
    echo '<title>'.$data['title'].'</title>';
    echo '<image>'.$data['image'].'</image>';
    echo '<caption>'.$data['caption'].'</caption>';
    echo '</entry>';
}
echo '</body>';

May need further tweaking but this should display your XML document just fine.

可能需要进一步调整,但这应该很好地显示您的XML文档。

Edit:

This will work if your array is fully populated, to do this you will want to populate the array using another loop, alternatively you can use a while loop to echo out all the rows like the following:

如果您的数组已完全填充,这将起作用,为此您需要使用另一个循环填充数组,或者您可以使用while循环来回显所有行,如下所示:

while ($row = mysql_fetch_assoc($result)) {
    echo '<entry>';
    echo '<title>'.$row['database column name'].'</title>';
    echo '<image>'.$row['database column name'].'</image>';
    echo '<caption>'.$row['database column name'].'</caption>';
    echo '</entry>';
}

Obviously you must replace 'database column name' with the relevant column names from your MySQL database. good luck, and let me know how you get on.

显然,您必须使用MySQL数据库中的相关列名替换“数据库列名”。祝你好运,让我知道你是怎么过的。

#1


0  

echo '<callback xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema.xsd">
<body>'; 
foreach($results['entry'] as $data{
    echo '<entry>';
    echo '<title>'.$data['title'].'</title>';
    echo '<image>'.$data['image'].'</image>';
    echo '<caption>'.$data['caption'].'</caption>';
    echo '</entry>';
}
echo '</body>';

May need further tweaking but this should display your XML document just fine.

可能需要进一步调整,但这应该很好地显示您的XML文档。

Edit:

This will work if your array is fully populated, to do this you will want to populate the array using another loop, alternatively you can use a while loop to echo out all the rows like the following:

如果您的数组已完全填充,这将起作用,为此您需要使用另一个循环填充数组,或者您可以使用while循环来回显所有行,如下所示:

while ($row = mysql_fetch_assoc($result)) {
    echo '<entry>';
    echo '<title>'.$row['database column name'].'</title>';
    echo '<image>'.$row['database column name'].'</image>';
    echo '<caption>'.$row['database column name'].'</caption>';
    echo '</entry>';
}

Obviously you must replace 'database column name' with the relevant column names from your MySQL database. good luck, and let me know how you get on.

显然,您必须使用MySQL数据库中的相关列名替换“数据库列名”。祝你好运,让我知道你是怎么过的。