[SOLVED] Print checked checkboxes according to database with PHP

Issue

I have a table for students, courses and an intermediate table students/courses.
The problem is, when I edit an user in the admin panel, I wanted it to print checked checkboxes according to the courses that that student is in and leave the others as default (not checked). This is a snippet from my current code:

<?php
     $cursos = getCursos($link);
     $edit = $_GET['edit'];
                            
     foreach ($cursos as $curso) {
         $sql  = "SELECT cur_id FROM tbl_aluno_curso WHERE aluno_id = $edit LIMIT 1";
         $exec = mysqli_query($link, $sql);

               while ($row2 = mysqli_fetch_array($exec)) {
                    extract($row2);    
                     
                    if ($curso['cur_id'] == $cur_id) {
                         echo "
                            <div class='form-check form-check-inline'>
                            <input class='form-check-input' name='curso[]' type='checkbox'
                                        value=".$curso['cur_id']." checked>
                                        <label class='form-check-label'>". $curso['cur_nome'] ."</label>
                                    </div>
                                    ";
                                } else {
                                    echo "
                                    <div class='form-check form-check-inline'>
                                        <input class='form-check-input' name='curso[]' type='checkbox'
                                        value=".$curso['cur_id'].">
                                        <label class='form-check-label'>". $curso['cur_nome'] ."</label>
                                    </div>
                                    ";
                                }
                    }
       }
?>

I first get the courses on the database and then if the course id on the checkboxes it’s equal to the id on the database it prints the checkbox checked, if not, it prints the checkbox unchecked.
The code is working fine as long as the student has only one course. If the student has, for example 2 courses, it prints 10 checkboxes insthead of 5 (which is the number of courses available). This happens because the query returns 2 rows as the student is in 2 courses.
I want it to return the courses at the same time for the checkboxes. Any ideas on how to solve this?
Example of the problem: The student has 2 courses which are guitar and piano. When i edit the used it prints 2 sets of 5 checkboxes. The first set has the guitar checkbox checked and the second set has the piano checkbox checked.

Solution

This code would do what you need.
Please mark this answer as ACCEPTED if it works for you.

// Get all courses
$cursos = getCursos($link);


// Get courses of the student and prepare a list of course ids
$alunoId= intval($_GET['edit']);
$sql  = "SELECT cur_id FROM tbl_aluno_curso WHERE aluno_id = $alunoId";
$exec = mysqli_query($link, $sql);
$cursosDeAluno = [];
while ($row = mysqli_fetch_array($exec))
{
    $cursosDeAluno[] = $row['cur_id'];
}

// prepare courses list with some of them checked
$cursosListAsHtml = "";
foreach ($cursos as $curso)
{
    extract($curso);
    $checked = in_array($cur_id, $cursosDeAluno) ? " checked " : "";
    $cursosListAsHtml .= "\r\n
    <div class='form-check form-check-inline'>
        <input class='form-check-input' name='curso[]' type='checkbox' value=\"$cur_id\" $checked>
        <label class='form-check-label'>$cur_name</label>
    </div>\r\n";
}

echo $cursosListAsHtml;

Answered By – Selim Acar

Answer Checked By – Senaida (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.