0

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       │
└─────────┴─────────────────┘
Cœur
  • 37,241
  • 25
  • 195
  • 267
Dexter
  • 7,911
  • 4
  • 41
  • 40
  • 3
    "I tried many methods from internet" And what are those things that you tried? Show them in your quesiton. – Patrick Q Mar 30 '18 at 15:02
  • There's nothing in your code that indicates that you're trying to check the checkboxes. – M. Eriksson Mar 30 '18 at 15:02
  • It is unclear what your issue is. Are you trying to add the `checked='checked'` to each `input` based on some condition? What condition? Why not just use a ternary? What have you tried that doesn't work? – IncredibleHat Mar 30 '18 at 15:03
  • @IncredibleHat I have edited the question and added what i was looking for at the end. – Dexter Mar 30 '18 at 15:26

2 Answers2

1

Your inner loop will only work the first time. The second time through the outer loop, you'll have fetched all the results from the second query, so the inner loop will stop immediately.

There's no need for two queries and two loops. The first query returns all the courses. You just need to check whether the columns from the enrollment table are checked. But for that to work properly, you need to move enrollments.enrollment_id into the ON clause (see Return default result for IN value regardless).

You also need to change the order of the LEFT JOIN. Since you want all courses, it should be courses LEFT JOIN enrollments.

<?php
$sql = "SELECT courses.*, enrollments.* 
        FROM courses
        LEFT JOIN enrollments 
        ON enrollments.courses=courses.course_id AND enrollments.enrollment_id = '$id'";
$result = mysqli_query($con, $sql);
while ($row = mysqli_fetch_assoc($result)) {
    $courseName = $row['course_name'];
    $courseId = $row['course_id'];
    $checked = $row['enrollment_id'] == NULL ? "" : "checked";
    echo "<input type='checkbox' name='courses[]' value='$courseId' id='$courseId' $checked>";
    echo "<label for='$courseName'>$courseName</label><br/>";
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Ahh! i did not notice the courses.* thanks for that. Whats wrong with this. `SELECT enrollments.enrollment_id, enrollments.courses FROM enrollments LEFT JOIN courses ON enrollments.courses=courses.course_id WHERE enrollments.enrollment_id = 4` – Dexter Mar 30 '18 at 15:24
  • Read the question I linked to for the explanation of why it has to be in `ON` instead of `WHERE`. – Barmar Mar 30 '18 at 15:26
  • Your code does not selects all - tested. It selects based on the course id, but i want something that is based on enrollment id. 1. by default all the checkboxes are visible 2. based on the where clause the courses gets checked.. Iam sorry for the confusion. – Dexter Mar 30 '18 at 15:30
  • 1
    Ah, I see the problem, the LEFT JOIN is backwards. – Barmar Mar 30 '18 at 15:35
  • It works but only the first value gets selected. is it because i save the checkbox values in the database in a single column? The values are saved in the database as `courses` and the values are `26, 24, 21, 20`. Out of this only the 26 gets selected. – Dexter Mar 30 '18 at 15:55
  • Comma-separated lists are bad design, you should normalize your design. – Barmar Mar 30 '18 at 15:55
  • See https://stackoverflow.com/questions/25611999/sql-join-tables-where-1-column-has-comma/25612053#25612053 for how to join with a comma-separated list. – Barmar Mar 30 '18 at 15:56
  • Let me check then. – Dexter Mar 30 '18 at 15:56
0

@Patrick Q - I did tried lot of method from the internet. Because the code was messy and i kept on commenting and deleting it, i could not able to get the post it here.

@Magnus Eriksson and @IncredibleHat - Sorry for the confusion, i will take care next time.

@Barmar - MANY THANKS. :)

Here's the final working code. I had two major issues Normalization and the not using FK. Because i was testing quick i created the table without setting the FK. I was able to select a checkbox value for one item using @barmar method. So i know i was close. I delete all the tables and created from scratch.

There are lots of answers in the internet for selecting value for the static dropdown, but related to dynamic generated checkbox from another table and selecting the value, i could not find.

1. Checkbox are generated from another table

while($row = mysqli_fetch_assoc($result)){
    $courseName = $row['course_name'];
    $courseId = $row['course_id'];
    echo "<input type='checkbox' name='courses[]' value='$courseId' id='$courseName' autocomplete='off'>";
    echo "<label for='$courseName'>$courseName</label><br/>";
}

2.Editing the checkbox with value selected

while ($row = mysqli_fetch_assoc($result)) {
    $studentid = $row['studentid'];
    $courseId = $row['courseId'];
    $courseName = $row['courseName'];

    $checked = $studentid == NULL ? "" : "checked='checked'";
    echo "<input type='checkbox' name='courses[]' value='$courseId' id='$courseId' $checked autocomplete='off'>";
    echo "<label for='$courseId'>$courseName</label><br/>";
}

Hope this helps someone who searching for the checkbox selected value from dynamic table.

Dexter
  • 7,911
  • 4
  • 41
  • 40