0

我有 2 张桌子,第一个存储餐厅信息,第二个存储每个服务的菜肴。它们使用 res_id 链接。

1) info_main [id, res_id, res_name,res_pc] 2) 菜肴 [id,dishName,price,res_id(外键)]

我的 SQL 查询是

$query = "SELECT *  FROM info_main LEFT JOIN dishes ON info_main.res_id = dishes.res_id"; 

我将查询结果插入到一个工作正常的 xml 文件中。下面是代码:

    $query = "SELECT *  FROM info_main LEFT JOIN dishes ON info_main.res_id = dishes.res_id";
$result = mysql_query($query);

if (!$result) {
  die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");

echo '<markers>';

// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  // ADD TO XML DOCUMENT NODE
  echo '<marker> ';

      echo '<detail1>';
        echo '<resdetails ';
            echo 'name="' . parseToXML($row['res_name']) . '" ';
            echo 'id="' . parseToXML($row['res_ID']) . '" ';
            echo 'pc="' . parseToXML($row['res_pc'] ). '" ';
        echo '/>';

        echo '<dishdetails ';
            echo 'name="' . parseToXML($row['dishName']) . '" ';
            echo 'price="' . parseToXML($row['price']) . '" ';
        echo '/>';

      echo '</detail1>';

      echo '</marker>';
    }

这很好,但是如果一家餐馆在数据库中有 3 道菜,那么 xml 会创建 3 个节点: 像这样:

xml文件放置

我想要像这样的xml结构

<detail1>
<resdetails name="spoted dog" id="xyz" pc="xyz"/>
<dishdetails name="bean burger" price="1" />
<dishdetails name="cheese and tomato panini" price="3" />
<dishdetails name="veg salad" price="2" />
</details1>

我不知道如何实现上述 xml 结构。非常感谢您的帮助。谢谢

4

3 回答 3

1
 $query = "SELECT *  FROM info_main LEFT JOIN dishes ON info_main.res_id = dishes.res_id";
$result = mysql_query($query);

if (!$result) {
  die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");

echo '<markers>';

// Iterate through the rows, printing XML nodes for each
$resname = "";
while ($row = @mysql_fetch_assoc($result)){
  // ADD TO XML DOCUMENT NODE
   if ($resname!=$row['res_name']) {
   //restname isn't populated or doesn't match current so output new headers
     echo '<marker> ';
      echo '<detail1>';
        echo '<resdetails ';
            echo 'name="' . parseToXML($row['res_name']) . '" ';
            echo 'id="' . parseToXML($row['res_ID']) . '" ';
            echo 'pc="' . parseToXML($row['res_pc'] ). '" ';
        echo '/>';
   }
        //this bit needs to always happen
        echo '<dishdetails ';
            echo 'name="' . parseToXML($row['dishName']) . '" ';
            echo 'price="' . parseToXML($row['price']) . '" ';
        echo '/>';


   if ($resname!=$row['res_name']) {
   //restname isn't populated or doesn't match current so output new headers
      echo '</detail1>';

      echo '</marker>';
   }
   $resname = $row['res_name'];  //set resname to this res_name as this is our check to see if we've already put out required headers for this item that way every change it'll put this back in
    }

像这样的东西(注意可能需要整理一下)

于 2013-11-14T16:13:02.440 回答
0

对于该结构,只需执行以下操作:

  echo '<marker> ';

  echo '<detail1>';

 while ($row = @mysql_fetch_assoc($result)){

 echo '<dishdetails ';
        echo 'name="' . parseToXML($row['dishName']) . '" ';
        echo 'price="' . parseToXML($row['price']) . '" ';
    echo '/>';

 }

echo '</detail1>';

  echo '</marker>';

真正不同的是,我将您的一部分代码移出 while 循环。

于 2013-11-14T16:13:52.383 回答
0

Atlast 我得到了这个工作,我使用了两个查询,一个得到不同的 restarant,另一个得到菜名。我在主循环中使用了循环。下面是代码。

$query = "SELECT DISTINCT  *  FROM info_main";

$result = mysql_query($query);


if (!$result) {
  die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");

echo '<markers>';

// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  $id=$row['res_id'];
  // ADD TO XML DOCUMENT NODE
  echo '<marker> ';

      echo '<detail1>';
        echo '<resdetails ';
            echo 'name="' . parseToXML($row['res_name']) . '" ';
            echo 'id="' . parseToXML($id) . '" ';
            echo 'pc="' . parseToXML($row['res_pc'] ). '" ';
        echo '/>';

        $dishes = "SELECT * FROM dishes WHERE res_id = '" . $id . "'";

        $dish = mysql_query($dishes);

        while ($row2 = @mysql_fetch_assoc($dish)){
          $id2 = $row2['res_id'];

            echo '<dishdetails ';
                echo 'name="' . parseToXML($row2['dishName']) . '" ';
                echo 'price="' . parseToXML($row2['price']) . '" ';
            echo '/>';

        }

      echo '</detail1>';

      echo '</marker>';
    }
echo '</markers>';
于 2013-11-16T23:50:27.623 回答