2013-11-14 91 views
0

我有两个表,第一个存储餐厅信息和第二个商店的菜肴在每个服务。它们使用res_id链接。php和mysql到xml

1)info_main [ID,RES_ID,RES_NAME,res_pc] 2)菜[ID,dishName,价格,RES_ID(外键)

我的SQL查询

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

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

这项工作很好但是如果餐厅有3个菜在数据库中,然后XML创建3个节点:事情是这样的:

xml file putput

我想要的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结构的方式。非常感谢您的帮助。由于

+0

所以......你只是想要所有的行中的一部分''? –

+0

您需要循环一次才能创建您的关键数据,即重新调配,然后再次循环将菜肴带出。或者检查使用的最后一个水库名称,以便它不重复它 – Dave

回答

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

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

+0

我试过这个代码,它给了我在docuemnt结尾的太多信息的错误。当我看到XML构建。这个结构就像这不是我想要的 – Ash

+0

太多的信息在文档的末尾是一个未封闭的标签或文档结尾的回车也应该已经把detail1标签内的碟子细节看起来像resdetails标签没有关闭需要一个/因为我说它的关闭不是100%,虽然需要一些整理,但它的90% – Dave

+0

抱歉@Dave,你是对的,标记标签在最后未被封闭,我的错,但是结构和上面提到的一样:(你可以看看这个:http://talentedash.co.uk/veggps/php_to_xml.php – Ash

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

+0

但是这对于多重限制器来说不起作用,因为他使用了连接,因此他将为每个添加的菜肴返回相同的资源细节,因此他只需要在新的res_name的第一次迭代中放置detail1标签,其余的将保留一样。 – Dave

+0

你的权利,没有想到通过,只是看着“他想要什么”。戴夫的答案在上面100%正确。 –