How I can show the data of groups in text box

Hello
I have 2 tables in the database which is the student has a student registration number, student names and groupId and student_group have groupId and projectName
and in Frontend, I get all groups names from the database as group A, B, C, etc in the dropdown list and if I select Group A I want to show the Registration number in one text box, Also show the Student Names mean that group all student names in second textbox and the project name in third textbox and it should be in PHP.
the coding I write for groups but what should I do for textbox which show the above mentioned data:

  <div class="form-group has-feedback">
                               <select name="groups" class="form-control" required>
                                <option>Choose Group</option>
                                   
                                    <?php
                                    $sqlGet="SELECT * FROM `student_group` ORDER BY `student_group`.`Groups` ASC";
                                    
                                    $result = $conn->query($sqlGet);
                                    if ($result->num_rows > 0) {
                                        while ($row = $result->fetch_assoc()) { ?>
                                            <option value="<?php echo $row['groupId'];?>"><?php echo $row['Groups']; ?></option>
                                        <?php
                                        }
                                    }
                                    ?>
                                </select>
                            </div>**

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.

It Is right but I want Registration Number, Student Names(3 Members), and Project table in each text

Each student has a registration number correct? This is what is shown to the user along with the name e.g. 11,12, 13. What would be the reason to have these in separate inputs when they identify the same person?
OR
are you saying you would like registration number, student name AND project “Shown” in ONE input. i.e.“in each text”??? That’s a little unclear. “Each in it’s own text” or “all in one text”.

BTW, what is the purpose of this form?
I can’t think of a good reason why you’d want all data to be put into a single input.
I suppose if you really want to throw everything into a textarea you could do something like this (based on the data and variables already set in my example).

<div class="col-lg-4">
	<?php	
	if(!empty($groupId)):
		$textdata = "Project: ".$projectName."\r\r";
		
		if(!empty($data[$groupId]['Students'])): 
			$textdata .= "Students: \r";
			foreach($data[$groupId]['Students'] as $reg_number => $stu_name):
				$textdata .= $reg_number.' '.$stu_name."\r"; 
			endforeach; 
		endif;
		echo '<textarea name="groupdata" class="form-control">'.$textdata.'</textarea>'."\r";
	endif;
	?>
</div>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.