lundi 31 août 2015

Automatically check a checkbox if data is in database

I am creating an edit page right now where an admin can edit a few settings inside the database from the admin panel. Everything is working so far except the work with checkboxes.

I created four checkboxes and everytime a checkbox is checked, the data will be added to my database as a string and inside the database those values are seperated with an ","! Now I want to make an edit page where admin can change those settings.

Therefore I created the following:

<div class="form-group">
 <label class="control-label col-md-3 col-sm-3 col-xs-12">Menüarten:</label>
    <div class="col-md-9 col-sm-9 col-xs-12">
    <input type="checkbox" name="menuearten[]" value="Menü traditionell" <?php if($_SESSION['data']['menueart'] == 'Menü traditionell') echo 'checked = "checked"' ?>> 1. Menü traditionell <? if ($_SESSION['data']['menueart'] == "Menü traditionell") echo "<span class='label label-success'>Aktuelle Auswahl</span>"; ?><br />
    <input type="checkbox" name="menuearten[]" value="Menü vegetarisch" <?php if($_SESSION['data']['menueart'] == 'Menü vegetarisch') echo 'checked = "checked"' ?>> 2. Menü vegetarisch <? if ($_SESSION['data']['menueart'] == "Menü vegetarisch") echo "<span class='label label-success'>Aktuelle Auswahl</span>"; ?><br />
    <input type="checkbox" name="menuearten[]" value="Menü Nudelgerichte"<?php if($_SESSION['data']['menueart'] == 'Menü Nudelgerichte') echo 'checked = "checked"' ?>> 3. Menü Nudelgerichte <? if ($_SESSION['data']['menueart'] == "Menü Nudelgerichte") echo "<span class='label label-success'>Aktuelle Auswahl</span>"; ?><br />
    <input type="checkbox" name="menuearten[]" value="Menü Gebackenes"<?php if($_SESSION['data']['menueart'] == 'Menü Gebackenes') echo 'checked = "checked"' ?>> 4. Menü Gebackenes <? if ($_SESSION['data']['menueart'] == "Menü Gebackenes") echo "<span class='label label-success'>Aktuelle Auswahl</span>"; ?><br />

With $_SESSION['data']['menueart'] I get all my data from the database. This is working already for other fields too. My code for that is:

$user_id = $_GET['id'];
$get_schulen = "SELECT * FROM schule WHERE id = '$user_id'";
$result_get_schulen = mysqli_query($connect, $get_schulen);
$_SESSION['data'] = mysqli_fetch_assoc($result_get_schulen);

Now my problem is, that I want to check all checkboxes when an admin visits the edit page which are already inside my database. Here is a picture of one of my current entry: http://ift.tt/1fR9F7O

So therefore if I am at my edit page, the first two checkboxes should be checked but they aren´t. Can someone tell me what is wrong with my IF condition or what should I change so that if a string exists in my database already, than the checkbox is checked automatically.




Aucun commentaire:

Enregistrer un commentaire