Get checked on checkbox select from database

Viewed 82

im working to edit data, and I retrieve data from the database. but when I tried to retrieve data in the form of a checkbox implementation, I experienced a loss in doing so. My checkbox can't check auto based on the data in my database.

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

as you can see my checkbox still 0, this must be got checked

enter image description here

when i add this code

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 } ?>

i got error like this

enter image description here

maybe someone can help me ? i just need got check when value same like my database

my database data like this enter image description here

1 Answers

simple answer replace this line

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

with this code

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

so your code will be like this

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 } ?>
Related