這是將兩個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