Christoph C.
Christoph C.

Reputation: 938

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://awesomescreenshot.com/0b5573hi81

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.

Upvotes: 0

Views: 829

Answers (2)

MaggsWeb
MaggsWeb

Reputation: 3027

You need to look a 'string functions' and use something like str_pos http://php.net/manual/en/function.strpos.php or stristr http://php.net/manual/en/function.stristr.php

eg:

(stristr($_SESSION['data']['menueart'],'Menü traditionell') ? 'checked = "checked"' : '' );

This is not a great way to store multiple values in a db column. Have you considered a second table storing each 'selection' and an identifier. It will make your life easier as it will be simpler to edit and delete individual values, and be much less prone to errors.

Upvotes: 1

user4628565
user4628565

Reputation:

probably there may be space issue,check with trim()

<input type="checkbox" name="menuearten[]" value="Menü traditionell" <?php if($_SESSION['data']['menueart'] == 'Menü traditionell') echo 'checked = "checked"' ?>> 

to

<input type="checkbox" name="menuearten[]" value="Menü traditionell" <?php if(trim($_SESSION['data']['menueart']) == 'Menü traditionell') echo 'checked = "checked"' ?>> 

and also, this line has to be changed,

echo 'checked = "checked"'

to

echo 'checked = checked'

Upvotes: 0

Related Questions