If I understand correctly, each student is assigned to a Group and will have ONE project name based on the selected Group ID. So once the Group is selected (in first select), the second select will show all students assigned to this group for user to select from and the third input (readonly) is populated with the Project Name.
Based on this I would do a joined query to gather all students using groupId as the join ON condition.
$sqlGet = "SELECT
sg.groupId
, sg.Groups
, sg.projectName
, st.registration_number
, st.student_name
FROM `student_group` AS sg
LEFT JOIN `student` AS st
ON st.groupId = sg.groupId
ORDER BY sg.Groups ASC";
The data you would have from the student_group
table are of course the groupId
, Groups
and projectName
. This information then repeats for each student
table record for this groupId
. Example:
Array
(
[groupId] => 1
[Groups] => Group A
[projectName] => Project A
[registration_number] => 11
[student_name] => student A
)
The problem with this is again if you have 30 students you have 30 rows of data. But if you structure a data array using both database fields and known named fields you can build a very structured usable array to work with.
I use the groupId
as the primary array KEY and hard code the keys ‘Groups’, ‘projectName’ and ‘Students’, which in turn will be an array of students with their ‘registration_number’ as their KEY and the ‘student_name’ as the VALUE.
Array
(
[1] => Array
(
[Groups] => Group A
[projectName] => Project A
[Students] => Array
(
[11] => student A
[12] => student B
[13] => student C
)
)
As I don’t want an undefined array under ‘Students’ if no students have been assigned (yet) I will wrap this portion of data building in an IF condition checking for the registration_number
. Here’s an example of what I am talking about.
$data = array();
$sqlGet = "SELECT
sg.groupId
, sg.Groups
, sg.projectName
, st.registration_number
, st.student_name
FROM `student_group` AS sg
LEFT JOIN `student` AS st
ON st.groupId = sg.groupId
ORDER BY sg.Groups ASC";
$result = $conn->query($sqlGet);
while ($row = $result->fetch_assoc()){
$data[$row['groupId']]['Groups'] = $row['Groups'];
$data[$row['groupId']]['projectName'] = $row['projectName'];
if(!empty($row['registration_number'])):
$data[$row['groupId']]['Students'][$row['registration_number']] = $row['student_name'];
endif;
}
This structured date array can then be looped through and used on all three inputs. By adding an onchange event to submit the form to your first group select, you then have $_POST['groups']
to define which students to show and the project name. It would go something like this.
<?php
$groupId = (!empty($_POST['groups']) && array_key_exists($_POST['groups'],$data) ? $_POST['groups'] : '');
$projectName = (!empty($_POST['groups']) && array_key_exists($_POST['groups'],$data) ? $data[$_POST['groups']]['projectName'] : '');
?>
<form action="" method="post">
<div class="form-group has-feedback">
<div class="col-lg-4">
<select name="groups" class="form-control" onchange="this.form.submit()" required>
<option>Choose Group</option>
<?php
if(!empty($data)):
foreach($data as $g_id => $ar):
$selected_group = (!empty($groupId) && $groupId == $g_id ? ' selected="selected"' : '');
echo '<option value="'.$g_id.'"'.$selected_group.'>'.$data[$g_id]['Groups'].'</option>'."\r";
endforeach;
endif;
?>
</select>
</div>
<div class="col-lg-4">
<select name="student" class="form-control" required>
<option>Choose Student</option>
<?php
if(!empty($data[$groupId]['Students'])):
foreach($data[$groupId]['Students'] as $reg_number => $stu_name):
$selected_student = (!empty($_POST['student']) && $_POST['student'] == $reg_number ? ' selected="selected"' : '');
echo '<option value="'.$reg_number.'"'.$selected_student.'>'.$reg_number.' '.$stu_name.'</option>'."\r";
endforeach;
endif;
?>
</select>
</div>
<div class="col-lg-4">
<input type="text" name="project" class="form-control" value="<?php echo $projectName;?>" placeholder="Project" style="background-color:white;" readonly />
</div>
</div>
</form>
By selecting the group you then see the students under this group showing both the registration number and the student name and the project name populated into the third input.