lundi 5 février 2018

How to echo 'checked' if checkbox is checked

In the "edit view" of my form I have a list of checkboxes and I want to check the ones which are in the table book_have_languages (notice that one book can have more than one language).

Query:

<?php $query_idi = mysqli_query($mysqli, "SELECT id_book, id_language FROM 
book_have_languages WHERE id_book='$_GET[id]';")
                                      or die('error: '.mysqli_error($mysqli));
$data_idi = mysqli_fetch_assoc($query_idi);
?>

Checkboxes:

<label>Idiomes</label>
   <?php
      $query_c = mysqli_query($mysqli, "SELECT id_language, name_language 
                                        FROM languages ORDER BY name_language;")

      while ($data_c = mysqli_fetch_assoc($query_c)) {
        echo"<div class='checkbox'><label><input type='checkbox' name='languages[]' value='$data_c[id_language]'";

      if ($data_idi['id_language']==$data_c['id_language']){
         echo "checked >$data_c[name_language]</label></div>";
      }else{
          echo ">$data_c[name_language]</label></div>";
        } 
      }
    ?>

Result:

If my table is:

id_book | id_language

3 - 2

3 - 5

3 - 6

Only is checked the first one:

<div class='checkbox'><label><input type='checkbox' name='languages[]' value='2'checked >English</label>




Aucun commentaire:

Enregistrer un commentaire