【问题标题】:php and mysql to xmlphp 和 mysql 到 xml
【发布时间】:2013-11-14 16:04:49
【问题描述】:

我有 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结构

<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 结构。非常感谢您的帮助。谢谢

【问题讨论】:

  • 所以...你只是想要一个&lt;detail&gt;的所有行的一部分吗?
  • 您需要循环一次以创建您的关键数据,即 resdetails,然后再次循环以取出菜肴。或检查最后使用的 res 名称,以免重复

标签: php mysql sql xml


【解决方案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
    }

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

【讨论】:

  • 我试过这段代码,它在文档末尾给了我一个错误信息太多。当我看到 xml 构建时。结构类似于 这不是我想要的
  • too much info at the end is an unclosed tag or a return at the end of document is also it should have put the disc details inside the detail1 tag and looks like the resdetails tag is not closed需要一个 / 就像我说的那样,它的收盘率不是 100%,虽然需要一些整理,但它的 90% 在那里
  • 对不起@Dave,你是对的,标记标签最后没有关闭,我的错,但是结构和上面提到的一样:(。你可以看看这个:talentedash.co.uk/veggps/php_to_xml.php
  • 检查您在数据库中的数据集,xml 格式完美,但您的数据库中似乎没有多道菜尝试直接对表运行查询?
【解决方案2】:

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

  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 循环。

【讨论】:

  • 但是这对于多个限制将不起作用,因为他正在使用 join 所以他将为添加的每道菜返回相同的 res 详细信息,所以他只需要在第一次迭代时放出 detail1 标签一个新的 res_name 其余部分保持不变。
  • 你说得对,没想到,只是看着“他想要什么”。上面戴夫的回答是 100% 正确的。
【解决方案3】:

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

$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>';

【讨论】:

    猜你喜欢
    • 2014-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多