0

I want to populate course list inside a dropdown but use the id from the student table to select its value in the edit page.

Database

course
+----+-------------+
| id | course_name |
+----+-------------+
| 1  | English     |
| 2  | Math        |
| 3  | Social      |
| 4  | Arts        |
| 5  | History     |
+----+-------------+

student
+----+--------+-----+--------+
| id | name   | age | course |
+----+--------+-----+--------+
| 1  | John   | 25  | 1      | /* English */
| 2  | Robert | 24  | 3      | /* Social */
| 3  | Nancy  | 21  | 4      | /* Arts */
+----+--------+-----+--------+  

I am populating the course table database inside a dropdown like this in the create new student page.

Create New student

$dropdown_query = "SELECT * FROM course";

// other codes

echo "<select name='course' value='' required/>";
    echo "<option value=''> Select the Course </option>";
    foreach (mysqli_query($con, $dropdown_query) as $row){
        echo "<option value='".$row['id']. "'> $row[course_name] </option>";
    }
echo "</select>";   

// other codes

HTML output

┌────┬────────┬─────┬─────────┬────────┐
│ id │  name  │ age │ course  │  Edit  │
├────┼────────┼─────┼─────────┼────────┤
│  1 │ John   │  25 │ English │ [Edit] │
│  2 │ Robert │  24 │ Social  │ [Edit] │
│  3 │ Nancy  │  21 │ Arts    │ [Edit] │
└────┴────────┴─────┴─────────┴────────┘

Edit Student page

I am stuck here

$id = '';
if( isset( $_GET['id'])) {
    $id = $_GET['id'];
}

$dropdown_query = "SELECT * FROM course";
$result = mysqli_query($con, $dropdown_query);

echo "<select name=course value=''> Course </option>";
    while ($row = mysqli_fetch_array($result)) {
        if($row['id'] == $id){
            echo "<option value='".$row['id']. "' selected> $row[course_name] </option>";
        } // if
        else{
            echo "<option value='" .$row['id']. "' >$row[course_name]</option>";
        } // else
    } // while
echo "</select>";

the code above generates the course inside the dropdown list, but the value is not selected. I want to combine two tables into one inside the dropdown.

Please help me!

4
  • is your option is showing in select ? Commented Feb 24, 2018 at 7:08
  • So for example you want your options to have like Student Name - Course Name? Commented Feb 24, 2018 at 7:09
  • 1
    try to put 1 2 or 3 in url parameter id, so that you get one option selected Commented Feb 24, 2018 at 7:11
  • @TarangP Yes it is showing. The problem is, it is not selected the value based on the id. Commented Feb 24, 2018 at 8:06

2 Answers 2

1

This is assuming you have an object $student which is the row from students corresponding to the current user.

$dropdown_query = "SELECT * FROM course";
$courses = mysqli_query($con, $dropdown_query);

echo '<select name="course">';
    while ($course = mysqli_fetch_array($courses)) {
        echo "<option value='{$course['id']}'".($student['course']==$course['id'] ? ' selected="selected"' : '').">{$course['course_name']}</option>";
    } // while
echo '</select>';
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help, the value is selected based on the id. But when i edit and save to new course it is not accepting the changes. Any idea.
Sorry, for the question above. it is fixed. It was the value issue.
0

Try This

echo "<select name=course value=''> Course </option>";
//ADD EMPTY ELEMENT SO YOU CAN SEE SELECTED ELement
echo "<option value=''> Your Course </option>";
    while ($row = mysqli_fetch_array($result)) {
        if($row['id'] == $id){
            echo "<option value='".$row['id']. "' selected> $row[course_name] </option>";
        } // if
        else{
            echo "<option value='" .$row['id']. "' >$row[course_name]</option>";
        } // else
    } // while
echo "</select>";

4 Comments

This will populate the course list dropdown but how does this select the correct value.
echo "<option value='".$row['id']. "' selected> $row[course_name] </option>" will select the correct value.
It does not. i am calling $dropdown = "SELECT * FROM course"; $result = mysqli_query($con, $dropdown); this looks at the course table but how does it know the proper id. Do i need to add a col in the course table as well?
Use` If($id == $row['id'] ) $var = "selected";`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.