【发布时间】:2023-03-23 13:00:03
【问题描述】:
我有这个 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
这给了我这个结果
我现在尝试将这些显示在类别中,并将类别名称作为每个类别的标题。
到目前为止,我在旧堆栈溢出帖子的帮助下获得了这段代码: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
)
)
)
我哪里错了?
谢谢。
【问题讨论】:
-
你的代码示例太小了,我们无法模仿,我想你可以给它添加更多内容或参考旧的 StackOverflow 链接。
-
代码和/或结果的图片不是很有用。请在您的问题中包含文本版本。
-
@JL2210 抱歉,这是第二次在这里发布问题,我不确定如何最好地显示结果表,所以我添加了图像。
-
@HueyMataruse 抱歉,这里是旧帖子的链接stackoverflow.com/questions/36458593/…
-
@HueyMataruse 我已经编辑了帖子并将结果图像换成了代码 sn-p。我仍然不确定如何在这里最好地显示一个 sql 表?