猿问

如何使用GROUP_CONCAT和EXPLODE函数在复选框中显示?

我是 PHP 新手。所以我这里有几个问题:


所以,这是我的“item_choice”表:


╔══════════╦═════════╦═══════════════╗

║  c_id    ║ item_id ║ choice_name   ║

╠══════════╬═════════╬═══════════════╣

║   1      ║ 100     ║   Set A       ║

║   2      ║ 100     ║   Set B       ║

║   3      ║ 100     ║   Set C       ║

║   4      ║ 100     ║   Set D       ║               

╚══════════╩═════════╩═══════════════╝

这是我的“item_subchoice”表:


╔══════════╦═════════╦═══════════════╦═══════════════╗

║  sc_id   ║ c_id    ║    option     ║    price      ║

╠══════════╬═════════╬═══════════════╬═══════════════╣

║   1      ║   1     ║   Fries       ║      4        ║

║   2      ║   1     ║   Coleslaw    ║      4        ║

║   3      ║   1     ║ Mac n Cheese  ║      5        ║

║   4      ║   2     ║   Fries       ║      4        ║               

╚══════════╩═════════╩═══════════════╩═══════════════╝

到目前为止,这是我的结果(方形的东西是复选框):


Set A:

╔═╗  

╚═╝ Fries,Coleslaw,Mac n Cheese  4,4,5

我想要实现的目标:


Set A:

╔═╗  

╚═╝ Friese  4,

╔═╗  

╚═╝ Coleslaw  4

╔═╗  

╚═╝ Mac n Cheese 5

这是我当前的代码:


<div class="form-row">


    <?php


    if (isset($_GET['itemid'])) {

        $choice_id = $_GET['itemid'];


        $get_choice_id = "SELECT

                            t1.*, 

                            GROUP_CONCAT(t2.sc_id) AS sc_ids,

                            GROUP_CONCAT(t2.option) AS choice_options,

                            GROUP_CONCAT(t2.price) AS choice_prices

                          FROM 

                            item_choice t1 

                          LEFT JOIN item_subchoice t2 ON t2.c_id = t1.c_id 

                          GROUP BY t1.c_id";


        }

    }


    ?>

</div>

我也尝试过explode函数和array_merge,但我无法让它工作。提前致谢!


BIG阳
浏览 111回答 1
1回答

不负相思意

您可能可以只进行正常的选择,并按集合排序 - 然后在循环结果时检查 PHP 中的集合是否已更改。另请注意,addon-checkbox不能是这样的 ID,因为它不是唯一的 - 可以有多行,并且元素的 ID 必须是唯一的。$current_set = null;$query = $con->query("SELECT ic.*, isc.sc_id, isc.option, isc.price&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FROM item_choice AS ic&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LEFT JOIN item_subchoice AS isc&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ON ic.c_id = isc.c_id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ORDER BY ic.choice_name");while ($row = $query->fetch_assoc()) {&nbsp; &nbsp; if ($current_set != $row['choice_name']) {&nbsp; &nbsp; &nbsp; &nbsp; $current_set = $row['choice_name'];&nbsp; &nbsp; &nbsp; &nbsp; echo '<h1>'.$current_set.'</h1>';&nbsp; &nbsp; }&nbsp; &nbsp; ?>&nbsp; &nbsp; <div class="form-group col-md-12">&nbsp; &nbsp; &nbsp; &nbsp; <hr style="height: 1px;">&nbsp; &nbsp; &nbsp; &nbsp; <label style='font-size:15px;'> Add Ons - <?= $row['choice_name'] ?>: </label>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="form-group col-md-12">&nbsp; &nbsp; &nbsp; &nbsp; <input type="checkbox addon-checkbox" name='check_list[]' onclick="check(this)" value="<?= $row['sc_ids'] ?>" />&nbsp; &nbsp; &nbsp; &nbsp; <label><?= $row['choice_options'] ?> + <?= $row['choice_prices'] ?></label>&nbsp; &nbsp; <?php}
随时随地看视频慕课网APP
我要回答