我有这个SQL查询来从2个表中获取数据,并根据匹配的ID将它们联接起来。
SELECT *
FROM bar_items
LEFT JOIN bar_cats ON
bar_cats.cat_id = bar_items.categories
ORDER BY bar_items.categories,
bar_items.item_name ASC
哪个给我这个结果
我现在正尝试在类别中显示这些类别,并以类别名称作为每个类别的标题。
到目前为止,借助旧的Stack溢出帖子,我已经有了这段代码:here
$data = array();
foreach($results_lists as $row){
$data[$row['cat_name']][$row['item_name']]['item_name'] = $row['item_name'];
$data[$row['cat_name']][$row['item_price']]['item_price'] = $row['item_price'];
$data[$row['cat_name']][$row['item_image']]['item_image'] = $row['item_image'];
} foreach($data as $category => $events){
echo $category.'<br>';
foreach($events as $event){
echo ' <> '.$event['item_name'].'<br>';
echo ' <> '.$event['item_price'].'<br>';
echo ' <> '.$event['item_image'].'<br>';
}
但它不符合我的需要,并且显示如下:(请注意空白的<>行,并在列表的最下方,即使在查询中返回了某些价格,也不会显示某些价格)
Beer and Bitter
<> John Smiths
<>
<>
<>
<> 2.50
<>
<>
<>
<> 1556315986_john-smiths.jpg
<> Sam Smiths
<>
<>
<>
<> 2.15
<>
<>
<>
<> 1556316430_Samuel_Smith_Brewery_logo.png
<> Stones
<>
<>
<>
<>
<> 1556316361_stones.jpg
<> Tetleys
<>
<>
<>
<>
<> 1556315794_tetleys.jpg
Lager
<> Bud Light
<>
<>
<>
<> 2.50
<>
<>
<>
<> 1556316641_bud-light.png
<> Carling
<>
<>
<>
<>
<> 1556316497_carling-white-011.png
<> Fosters
<>
<>
<>
<>
<> 1556316197_fosters.png
Cider
<> Strongbow
<>
<>
<>
<> 2.50
<>
<>
<>
<> 1556316131_Strongbow-Logo-1.png
编辑: 当我打印数组时,我得到这个:
Array
(
[Beer and Bitter] => Array
(
[John Smiths] => Array
(
[item_name] => John Smiths
)
[2.50] => Array
(
[item_price] => 2.50
)
[1556315986_john-smiths.jpg] => Array
(
[item_image] => 1556315986_john-smiths.jpg
)
[Sam Smiths] => Array
(
[item_name] => Sam Smiths
)
[2.15] => Array
(
[item_price] => 2.15
)
[1556316430_Samuel_Smith_Brewery_logo.png] => Array
(
[item_image] => 1556316430_Samuel_Smith_Brewery_logo.png
)
[Stones] => Array
(
[item_name] => Stones
)
[1556316361_stones.jpg] => Array
(
[item_image] => 1556316361_stones.jpg
)
[Tetleys] => Array
(
[item_name] => Tetleys
)
[1556315794_tetleys.jpg] => Array
(
[item_image] => 1556315794_tetleys.jpg
)
)
[Lager] => Array
(
[Bud Light] => Array
(
[item_name] => Bud Light
)
[2.50] => Array
(
[item_price] => 2.50
)
[1556316641_bud-light.png] => Array
(
[item_image] => 1556316641_bud-light.png
)
[Carling] => Array
(
[item_name] => Carling
)
[1556316497_carling-white-011.png] => Array
(
[item_image] => 1556316497_carling-white-011.png
)
[Fosters] => Array
(
[item_name] => Fosters
)
[1556316197_fosters.png] => Array
(
[item_image] => 1556316197_fosters.png
)
)
[Cider] => Array
(
[Strongbow] => Array
(
[item_name] => Strongbow
)
[2.50] => Array
(
[item_price] => 2.50
)
[1556316131_Strongbow-Logo-1.png] => Array
(
[item_image] => 1556316131_Strongbow-Logo-1.png
)
)
)
我要去哪里错了?
谢谢。
答案 0 :(得分:1)
我认为您正在使生活变得比实际需要的更加复杂。通过结果集的简单传递应该能够产生您希望的输出。
$last_cat = null;
foreach($results_lists as $row){
if ( $last_cat != $row['cat_name'] ) {
echo $row['cat_name'] . '<br>';
$last_cat = $row['cat_name'];
}
echo ' <> '.$row['item_name'].'<br>';
echo ' <> '.$row['item_price'].'<br>';
echo ' <> '.$row['item_image'].'<br>';
}