如何使用 GROUP_CONCAT 和 EXPLODE 函数在复选框中显示?
How to use GROUP_CONCAT and EXPLODE function to display in checkbox?
我是 PHP 的新手。所以我这里有几个问题:
所以,这是我的“item_choice”table:
╔══════════╦═════════╦═══════════════╗
║ c_id ║ item_id ║ choice_name ║
╠══════════╬═════════╬═══════════════╣
║ 1 ║ 100 ║ Set A ║
║ 2 ║ 100 ║ Set B ║
║ 3 ║ 100 ║ Set C ║
║ 4 ║ 100 ║ Set D ║
╚══════════╩═════════╩═══════════════╝
这是我的“item_subchoice”table:
╔══════════╦═════════╦═══════════════╦═══════════════╗
║ 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";
$query = mysqli_query($con, $get_choice_id);
foreach (($query) as $row) {
$choice_id = $row['c_id'];
$choice_name = $row['choice_name'];
$subchoice_id = $row['sc_ids'];
$subchoice_option = $row['choice_options'];
$subchoice_price = $row['choice_prices'];
echo"<div class='form-group col-md-12'>";
echo"<hr style='height:1px;'>";
echo"<label style='font-size:15px;'> Add Ons - $choice_name: </label>";
echo"</div>";
echo"<div class='form-group col-md-12'>";
echo"<input type='checkbox' id='addon-checkbox' name='check_list[]' onclick='check(this)'";
echo" <label value='$subchoice_id'>$subchoice_option +$subchoice_price</label>";
echo"</div>";
}
}
?>
</div>
我试过爆炸函数,也试过 array_merge,但我无法让它工作。提前致谢!
您可以只执行正常的 select,然后按集合排序 - 然后在遍历结果时检查 PHP 中的集合是否已更改。
另请注意,addon-checkbox
不能是这样的 ID,因为它不是唯一的 - 可以有多个行,并且元素的 ID 必须是唯一的。
$current_set = null;
$query = $con->query("SELECT ic.*, isc.sc_id, isc.option, isc.price
FROM item_choice AS ic
LEFT JOIN item_subchoice AS isc
ON ic.c_id = isc.c_id
ORDER BY ic.choice_name");
while ($row = $query->fetch_assoc()) {
if ($current_set != $row['choice_name']) {
$current_set = $row['choice_name'];
echo '<h1>'.$current_set.'</h1>';
}
?>
<div class="form-group col-md-12">
<hr style="height: 1px;">
<label style='font-size:15px;'> Add Ons - <?= $row['choice_name'] ?>: </label>
</div>
<div class="form-group col-md-12">
<input type="checkbox addon-checkbox" name='check_list[]' onclick="check(this)" value="<?= $row['sc_ids'] ?>" />
<label><?= $row['choice_options'] ?> + <?= $row['choice_prices'] ?></label>
<?php
}
我是 PHP 的新手。所以我这里有几个问题:
所以,这是我的“item_choice”table:
╔══════════╦═════════╦═══════════════╗
║ c_id ║ item_id ║ choice_name ║
╠══════════╬═════════╬═══════════════╣
║ 1 ║ 100 ║ Set A ║
║ 2 ║ 100 ║ Set B ║
║ 3 ║ 100 ║ Set C ║
║ 4 ║ 100 ║ Set D ║
╚══════════╩═════════╩═══════════════╝
这是我的“item_subchoice”table:
╔══════════╦═════════╦═══════════════╦═══════════════╗
║ 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";
$query = mysqli_query($con, $get_choice_id);
foreach (($query) as $row) {
$choice_id = $row['c_id'];
$choice_name = $row['choice_name'];
$subchoice_id = $row['sc_ids'];
$subchoice_option = $row['choice_options'];
$subchoice_price = $row['choice_prices'];
echo"<div class='form-group col-md-12'>";
echo"<hr style='height:1px;'>";
echo"<label style='font-size:15px;'> Add Ons - $choice_name: </label>";
echo"</div>";
echo"<div class='form-group col-md-12'>";
echo"<input type='checkbox' id='addon-checkbox' name='check_list[]' onclick='check(this)'";
echo" <label value='$subchoice_id'>$subchoice_option +$subchoice_price</label>";
echo"</div>";
}
}
?>
</div>
我试过爆炸函数,也试过 array_merge,但我无法让它工作。提前致谢!
您可以只执行正常的 select,然后按集合排序 - 然后在遍历结果时检查 PHP 中的集合是否已更改。
另请注意,addon-checkbox
不能是这样的 ID,因为它不是唯一的 - 可以有多个行,并且元素的 ID 必须是唯一的。
$current_set = null;
$query = $con->query("SELECT ic.*, isc.sc_id, isc.option, isc.price
FROM item_choice AS ic
LEFT JOIN item_subchoice AS isc
ON ic.c_id = isc.c_id
ORDER BY ic.choice_name");
while ($row = $query->fetch_assoc()) {
if ($current_set != $row['choice_name']) {
$current_set = $row['choice_name'];
echo '<h1>'.$current_set.'</h1>';
}
?>
<div class="form-group col-md-12">
<hr style="height: 1px;">
<label style='font-size:15px;'> Add Ons - <?= $row['choice_name'] ?>: </label>
</div>
<div class="form-group col-md-12">
<input type="checkbox addon-checkbox" name='check_list[]' onclick="check(this)" value="<?= $row['sc_ids'] ?>" />
<label><?= $row['choice_options'] ?> + <?= $row['choice_prices'] ?></label>
<?php
}