php - 选中从数据库中选择的复选框

标签 php html database mysqli

我正在编辑数据,并从数据库中检索数据。但是当我尝试以复选框实现的形式检索数据时,我在这样做时遇到了损失。我的复选框无法根据数据库中的数据自动选中。

<div class="form-group">
    <label>Fasilitas: </label>
        <div class="row skin skin-flat">
            <div class="col-md-4 col-sm-12">
                <?php
                    $gfasis =  (explode(", ",$i['gedung_fasilitas']));
                        $fasi = [
                                    1=>
                                    "Catering",
                                    "Dekorasi Pelaminan",
                                    "Photo & Video Akad Resepsi",
                                    "Album Kolase",
                                    "Makeup",
                                    "Mc / Pembawa Acara",
                                    "Weeding Organizer",
                                    "Entertainment",
                                    "Pakaian Pengantin",
                                    "Ruang Full AC",
                                    "Meja VIP",
                                    "Lighting",
                                    "Lcd Proyektor",
                                    "Tari Tradisional",
                                    "Photo Both",
                                    "Seragam Keluarga",
                                    "Seragam Orang tua",
                                    "Meja Akad nikah",
                                    "Buku Tamu",
                                    "Kotak Amplop",
                                    "Box Hantaran",
                                    "Free Menginap di Hotel",
                                    "Qoori Akad / Resepsi",
                                    "Ruang Hias",
                                    "Raung Tunggu Pengantin",
                                    "Beskap Pengantin",
                                    "Rental Mobil Pengantin",
                                    "Kursi sofa",
                                    "Meja makan prasmanan",
                                    "Gazebo Pintu Masuk",
                                    "Red Carpet"
                                ];


                    for($kk=1; $kk<=11; $kk++) { 
                ?>

                    <fieldset>
                        <input type="checkbox" id="<?= $kk; ?>" name="fasilitas[]" value="<?= $kk; ?>">
                        <label for="<?= $kk; ?>"><?= $fasi[$kk]; ?></label>
                    </fieldset>

                    <?php } ?>
            </div>

            <div class="col-md-4 col-sm-12">
                <?php
                    for($kk=12; $kk<=21; $kk++) { 
                ?>
                    <fieldset>
                        <input type="checkbox" id="<?= $kk; ?>" name="fasilitas[]" value="<?= $kk; ?>">
                        <label for="<?= $kk; ?>"><?= $fasi[$kk]; ?></label>
                    </fieldset>
                <?php } ?>

            </div>

                <div class="col-md-4 col-sm-12">
                    <?php
                        for($kk=22; $kk<=31; $kk++) { ?>
                    <fieldset>
                        <input type="checkbox" id="<?= $kk; ?>" name="fasilitas[]" value="<?= $kk; ?>">
                        <label for="<?= $kk; ?>"><?= $fasi[$kk]; ?></label>
                    </fieldset>
                    <?php } ?>
                </div>

            </div>
        </div>

你可以看到我的复选框仍然是 0,这必须被检查

enter image description here

当我添加这段代码时

for($kk=1; $kk<=11; $kk++) { ?>
<fieldset>
    <input type="checkbox" id="<?= $kk; ?>" name="fasilitas[]" value="<?= $kk; ?>" <?php if($gfasis[$kk]==$kk){echo "checked";} else {}?>>
    <label for="<?= $kk; ?>"><?= $fasi[$kk]; ?></label>
</fieldset>
<?php } ?>

我遇到这样的错误

enter image description here

也许有人可以帮助我? 我只需要检查值何时与我的数据库相同

我的数据库数据是这样的 enter image description here

最佳答案

简单的回答 替换这一行

<?php if($gfasis[$kk]==$kk){echo "checked";} else {}?>

使用此代码

 <?php if (in_array($kk, $gfasis)){  echo "checked";}?>

所以你的代码会是这样的

for($kk=1; $kk<=11; $kk++) { ?>
<fieldset>
    <input type="checkbox" id="<?= $kk; ?>" name="fasilitas[]" value="<?= $kk; ?>" 
    <?php if (in_array($kk, $gfasis)) {echo "checked";} else {}?>>
    <label for="<?= $kk; ?>"><?= $fasi[$kk]; ?></label>
</fieldset>
<?php } ?>

关于php - 选中从数据库中选择的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54167514/

相关文章:

php - MySQL 查询 - 选择所有帖子并计算每个帖子的评论

javascript - 客户端和服务器端编程有什么区别?

php - 使用 Zend Framework 应用程序将 subview 加载到 IFrame 中

javascript - 显示/隐藏图标折叠所有 div 的问题

html - 使圆形路径从顶部开始

mysql - 在 MySQL 中重新创建 UNI 和 MUL 索引

javascript - 如何在jquery中全局访问回调函数变量

html - 如何垂直对齐单选按钮?

c# - 不支持关键字 : 'attachdbfilename' - MDF Database File (C#)

SQL:指定列的冗余 WHERE 子句 > 0?