我需要通过解释代码来获得帮助,这样我才能理解如何处理多个表:
有几个名为
的表公寓,物业,类别,开发商
他们之间的关系如下:
category_id
与id
表categories
列同步
developer_id
与id
表developers
列同步
property_id
与id
表properties
列同步
我需要更新下面的PHP代码以使输出像那样
<?xml version="1.0" ?>
<list>
<property>
<title>apartments_title</title >
<price>apartments_price</price >
<type>properties_title</type>
<categoty>categories_title</categoty>
<developer>developer_title</developer>
</property>
</list>
我使用此代码从一个表中检索数据
header('Content-type: text/xml');
// INITIALIZE DOM OBJECT
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;
// CREATE ROOT AND APPEND TO DOCUMENT
$xmlRoot = $dom->createElement("list");
$xmlRoot = $dom->appendChild($xmlRoot);
// QUERY DATABASE
$db = new PDO('mysql:host=hostname;dbname=dbname','dbusername','password');
$stmt = $db->prepare("select * from apartments");
$stmt->execute();
// FETCH ROWS ITERATIVELY
while($row = $stmt->fetch()){
// APPEND property AS CHILD OF ROOT
$propertyNode = $xmlRoot->appendChild($dom->createElement('property'));
// APPEND CHILDREN TO Property
$propertyNode->appendChild($dom->createElement('title', $row['title']));
$propertyNode->appendChild($dom->createElement('price', $row['price']));
}
$stmt = null;
$db = null;
// OUTPUT TO SCREEN
echo $dom->saveXML();
答案 0 :(得分:1)
如评论中所述,您希望使用联接来获取所需的值。左连接确保包含来自apartments表的所有记录,而其他表(如果有)中的缺失值将返回为NULL。
SELECT a.title AS title, a.price AS price, p.title AS type,
c.title AS category, d.title AS developer
FROM apartments a
LEFT JOIN properties p ON (a.property_id = p.id)
LEFT JOIN categories c ON (a.category_id = c.id)
LEFT JOIN developers d ON (a.developer_id = d.id);
注意我已经为表(为了使SQL更清晰)设置了别名,并且在构建XML时也使用了列。您可以循环显示结果,而不是分别为每列调用createElement
。这是可能的,因为我已将fetch类型指定为关联数组,而不是默认的fetch类型,它既是关联的又是数字的。最后要注意的是,如果没有参数,则不需要prepare()
和execute()
语句。只需运行query()
即可获得相同的结果。
<?php
header('Content-type: text/xml');
// INITIALIZE DOM OBJECT
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;
// CREATE ROOT AND APPEND TO DOCUMENT
$xmlRoot = $dom->createElement("list");
$xmlRoot = $dom->appendChild($xmlRoot);
// QUERY DATABASE
$db = new PDO('mysql:host=hostname;dbname=dbname','dbusername','password');
$stmt = $db->query("SELECT a.title AS title, a.price AS price, p.title AS type, c.title AS category, d.title AS developer FROM apartments a LEFT JOIN properties p ON (a.property_id = p.id) LEFT JOIN categories c ON (a.category_id = c.id) LEFT JOIN developers d ON (a.developer_id = d.id);");
// FETCH ROWS ITERATIVELY
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
// APPEND property AS CHILD OF ROOT
$propertyNode = $xmlRoot->appendChild($dom->createElement('property'));
foreach ($row as $element=>$value) {
// APPEND CHILDREN TO Property
$propertyNode->appendChild($dom->createElement($element, $value));
}
}
// OUTPUT TO SCREEN
echo $dom->saveXML();
如果要将此数据提供给您自己的服务,您可能需要考虑使用JSON而不是XML。 JSON更简单的数据结构和PHP非常简洁的内置函数将使这个程序变得更小:
<?php
header('Content-type: application/json');
// QUERY DATABASE
$db = new PDO('mysql:host=hostname;dbname=dbname','dbusername','password');
$stmt = $db->query("SELECT a.title AS title, a.price AS price, p.title AS type, c.title AS category, d.title AS developer FROM apartments a LEFT JOIN properties p ON (a.property_id = p.id) LEFT JOIN categories c ON (a.category_id = c.id) LEFT JOIN developers d ON (a.developer_id = d.id);");
// FETCH ROWS
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC));
// OUTPUT TO SCREEN
echo json_encode($rows);