Show checkbox values from database in PHP

后端 未结 4 1806
天命终不由人
天命终不由人 2021-01-26 19:43

In Database I have a column \'language\' which have values English, Arabic, Urdu respectively. Now, I am gett

相关标签:
4条回答
  • 2021-01-26 20:30

    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>
    
    0 讨论(0)
  • 2021-01-26 20:31

    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.

    0 讨论(0)
  • 2021-01-26 20:34
    <?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>
    
    0 讨论(0)
  • 2021-01-26 20:39

    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>
    
    0 讨论(0)
提交回复
热议问题