Check Dynamic Checkbox (Checkbox) from a while inside the database

Asked

Viewed 97 times

1

Guys, I’m banging my head here with this... I have two tables in the bank, one corresponds to the department and another corresponds to the checkbox registered for that department, the problem is, how to bring these checkbox marked, for example, I need to validate them in the display, as "checked", that inside the while, but they are two different tables... I even managed to get halfway through, but it displays only one result. Follow below table and code, if anyone can give a help, I really appreciate.

$sql = "SELECT * FROM departamentos";
$rs = mysqli_query($conn, $sql);
while($departamento = mysqli_fetch_array($rs)){
                                    
$sql2 = "SELECT DISTINCT id_departamento FROM checkbox WHERE tipo='1' AND id_galeria_artigo='$id'";
$rs2 = mysqli_query($conn, $sql2);
$checkbox = mysqli_fetch_array($rs2)
?>
<label class="btn <?php if($departamento['id'] == $checkbox['id_departamento']) { echo 'active';}?>"  style="margin:5px">
     <input type="checkbox" name="check[]" value="<?php echo $departamento['id']?>" > <?php echo $departamento['title']?>
</label>
<?php
    }
?>

This is the table "checkbox":

,

Department table:

inserir a descrição da imagem aqui

Galeria Table:

inserir a descrição da imagem aqui

  • can post the other table? and the expected result?

  • The second SELECT always returns the same result within while

  • So Leo, the other table would be just the department, in this case the client registers a gallery, and this gallery needs to appear in the respective selected departments, these departments are selected via checkbox, and save in the table "checkbox", when the user will edit this gallery in the panel, I need to display the checkbox for the respective departments, and I need to check whether this checkbox was selected or not, to mark as checked, understand?

  • I understand, but without this table I cannot run the script to perform tests.

  • Got it... I top the two tables, the department, and the gallery, I believe is not so relevant, because it only takes the id of the selected gallery...

  • I drove here and returned this https://i.stack.Imgur.com/Bntfv.png

  • is because of the "check" in the input, I edited up there and took it, what makes the asset validation or is not the if above, if that department is active, it prints the "active"

  • That $id comes from where?

  • he took from the gallery, inside this gallery I have the departments to be selected.

  • I’m not seeing what would be the problem, the image I showed you is not the expected result? What would be the expected result?

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.