2

In Database I have a column 'language' which have values English, Arabic, Urdu respectively. Now, I am getting these values and show these values in checkboxes for update purposes. I have to check the checkbox if it matches the value with database value and then make it checked otherwise unchecked. But I am getting the wrong results.. Below code is working fine for just 1st value returned from database which is 'English' in my case and it is checked if it matches the database value but code is not working for other values and it remains unchecked even if it matches the database value.. Please help me..

Below is my code and image...

See Image For Error

PHP:-

$lang = $pdo->prepare("SELECT `language` FROM admin_panel_languages WHERE user_id=:user_id");
$lang->execute(array(":user_id"=>$user_id));
$lang_spoken=$lang->fetchAll(PDO::FETCH_ASSOC);
print_r($lang_spoken);

foreach($lang_spoken as $lang){

if($lang['language']=="English"){

?>

<label class="col-md-4">
<input type="checkbox" value="English" name="language[]" id='checkboxes' checked/> English </label>

<?php } else{ ?>

<label class="col-md-4">
<input type="checkbox" value="English" name="language[]" id='checkboxes'/> English </label>

<?php } break; } ?>

foreach($lang_spoken as $lang){

if($lang['language']=="Hindi"){

?>

<label class="col-md-4">
<input type="checkbox" value="Hindi" name="language[]" id='checkboxes' checked/> Hindi</label>

<?php } else{ ?>

<label class="col-md-4">
<input type="checkbox" value="Hindi" name="language[]" id='checkboxes'/> Hindi</label>

<?php } break; } ?>

Now, I have English,Hindi,Arabic in Array. But my code is only working for 1st element of array which is English and make is checked but for the rest of values it remain unchecked even if statement is matched. It is always going to else statement for rest of values.. Please tell me where is the problem. Thanks in advance..

4
  • how did you create language checkbox in front end when you add users? Commented May 10, 2016 at 7:20
  • @mujas By simple HTML (<input type="checkbox" value="English" name="language[]" id='checkboxes'>) etc.. Commented May 10, 2016 at 7:23
  • try to have it in one foreach loop Commented May 10, 2016 at 7:29
  • can you please paste your array below? Commented May 10, 2016 at 7:33

4 Answers 4

3

Try below code.

<?php
$checkedEnglish = $checkedHindi = 0;    
foreach($lang_spoken as $lang){
    if($lang['language']=="English"){
        $checkedEnglish = 1;
    }
    elseif($lang['language']=="Hindi")
    {
        $checkedHindi = 1;
    }
}
?>

<label class="col-md-4">
<input type="checkbox" value="English" name="language[]" id='checkboxes' <?php echo ($checkedEnglish == 1) ? "checked" : ""; ?>/> English </label>

<label class="col-md-4">
<input type="checkbox" value="Hindi" name="language[]" id='checkboxes' <?php echo ($checkedHindi == 1) ? "checked" : ""; ?>/> Hindi </label>
Sign up to request clarification or add additional context in comments.

Comments

3
<?php 

$lang_spoken = array();
$lang_spoken[0]['language'] = 'English';
$lang_spoken[1]['language'] = 'Hindi';
    $isEnglish = "";
    $isHindi = "";
foreach($lang_spoken as $lang){


    if($lang['language']=="English"){
        $isEnglish = "checked=checked";
    }

    if($lang['language']=="Hindi"){
        $isHindi = "checked=checked";
    }
}
?>

<label class="col-md-4">
<input type="checkbox" value="English" name="language[]" id='checkboxes' <?php echo $isEnglish; ?>/> English </label>

<label class="col-md-4">
<input type="checkbox" value="Hindi" name="language[]" id='checkboxes' <?php echo $isHindi; ?>/> Hindi</label>

Comments

2

You can use in_array() method. you have languages array with you.

$lang_spoken;

in your html file

<label class="col-md-4">
<input type="checkbox" value="English" name="language[]" id='checkboxes' <?php if (in_array("English", $lang_spoken))
{?> checked <?php } ?>/> English </label>

1 Comment

did you try this method?
1

Try this:

$lang = $pdo->prepare("SELECT `language` FROM admin_panel_languages WHERE user_id=:user_id");
$lang->execute(array(":user_id"=>$user_id));
$lang_spoken=$lang->fetchAll(PDO::FETCH_ASSOC);
print_r($lang_spoken);

$checkedEng = '';
$checkedHindi = '';

foreach($lang_spoken as $lang) {
    if (($lang['language'] == "English")) {
        $checkedEng = 'checked';
    } else if ($lang['language'] == "Hindi") {
        $checkedHindi = 'checked';
    }
}

<label class="col-md-4">
<input type="checkbox" value="English" name="language[]" id='checkboxes' <?php echo $checkedEng; ?>/> English </label>

<label class="col-md-4">
<input type="checkbox" value="Hindi" name="language[]" id='checkboxes' <?php echo $checkedHindi; ?>/> Hindi</label>

?>

Though I haven't tested the above code, but I think this one should work for you.

4 Comments

Its wrong. It will consider always last row value in $checkedEng, $checkedHindi.
@RuchishParikh, can you please explain it how?
If $lang_spoken have multiple records then it will consider last one.
@Junaidafzal, please check the latest one now. Sorry I missed that one user can have more than one spoken language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.