I am trying to get the value checked in the checkbox from the mysql. I can able to output the checked values. But i cannot make them check in the checkbox. I tried many methods from internet, but none of them seems to work. Maybe i am missing something here. Help me out.
$sql = "SELECT courses.*, enrollments.*
FROM enrollments LEFT JOIN courses
ON enrollments.courses=courses.course_id
WHERE enrollments.enrollment_id = '$id'";
$sql_c = "SELECT * FROM courses";
// query the result
$result = mysqli_query($con, $sql);
$result_c = mysqli_query($con, $sql_c);
// first loop calling using id
while($row = mysqli_fetch_assoc($result)){
$courses = $row['courses']; // only the selected values
// Second loop calling all the courses
while ($rowc = mysqli_fetch_array($result_c)) {
$courseName = $rowc['course_name']; // all the course names
$courseId = $rowc['course_id']; // all the course id
echo "<input type='checkbox' name='courses[]' value='$courseId' id='$courseId' autocomplete='off'>";
echo "<label for='$courseName'>$courseName</label><br/>";
} // loop 2
} // loop 1
Here's the output
echo $courses;
// 26, 24, 21, 20
echo $courseId." ";
// 18 19 20 21 22 23 24 26
How do i make these values checked.
This is what i am looking for
┌─────────┬─────────────────┐
│ [check] │ English │
│ [ ] │ French │
│ [ ] │ Sanskrit │
│ [check] │ Arts and Crafts │
│ [check] │ History │
│ [ ] │ Chemistry │
└─────────┴─────────────────┘