繁体   English   中英

SQL连接2个表并遍历结果

[英]SQL join 2 tables and loop through results

我有这个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 Overflow帖子的帮助下有此代码: 这里

    $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
                )

        )

)

我要去哪里错了?

谢谢。

我认为您正在使生活变得比实际需要的更加复杂。 通过结果集的简单传递应该能够产生您希望的输出。

$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>';
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM