这是将两个SQL查询合并到数组的正确方法吗?将两个SQL查询组合到一个数组中?
注:我知道SQL JOIN,但我需要使用两个查询。
见下文:现在
$query = "SELECT * FROM categories WHERE takeawayID = :TakeawayID";
$statement = $this->db->prepare($query);
$statement->bindValue(':TakeawayID', $takeawayID, PDO::PARAM_STR);
$statement->execute();
$data['rowCats'] = $statement->fetchall(PDO::FETCH_CLASS);
$categories = array();
foreach ($data['rowCats'] as $cat) {
$temp_categories = array();
$temp_categories['id'] = $cat->id;
$temp_categories['name'] = $cat->name;
$num = $cat->id;
$query = "SELECT * FROM items WHERE category_id = :category_id";
$statement = $this->db->prepare($query);
$statement->bindValue(':category_id', $num, PDO::PARAM_STR);
$statement->execute();
$data['rowItem'] = $statement->fetchall(PDO::FETCH_CLASS);
foreach ($data['rowItem'] as $Item) {
$temp_categories['item']['name'][] = $Item->name;
}
$categories[] = $temp_categories;
}
$类别阵列可以被传递到视图文件(模板)
在视图文件我应该可以做这样的事情:
<?php foreach($categories as $category): ?>
<table border=0 Cellspacing='0'>
<tr>
<td>
<?php echo $category['name']; ?>
</td>
</tr>
<?php foreach ($category['items'] as $item): ?>
<tr>
<td>
<?php echo $item['name']; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php endforeach; ?>
什么样的问题?它输出什么?你有错误信息吗? – Kaivosukeltaja 2011-04-05 16:59:40