gpt4 book ai didi

php - 按不同组sql将学生分开

转载 作者:行者123 更新时间:2023-11-29 11:06:18 25 4
gpt4 key购买 nike

如何分离学生并使用两个嵌套的for循环来存储数据..?

$jobID              =   85
$gd_individuals = 4 ;

$studentQuery = $conn->query(" select s.student_pid,i.email,s.student_email, s.student_fname,
s.student_lname,s.profile_pic from r_job_invitations i
LEFT JOIN tbl_students s ON i.email = s.student_email
where i.id_job = ".$jobID." and inv_res = 1");

//$studentResults = $studentQuery->fetch_assoc(); //total 8 students
$totalIndividuals = mysqli_num_rows($studentQuery); //total 8 students

$groups = 2 //which is coming from total/gd_individual 8/4=2

for($j = 0; $j<$groups; $j++) {

while($studentResults = $studentQuery->fetch_array()){ //total 8 studnets

echo "INSERT INTO r_test_group
(student_id,job_id,group_name)
values (".$studentResults['student_pid'].", ".$jobID.",Group".$j." )";


}
}
exit;
}

我想将第一名 4 名学生存储为 group1,第二名 4 名学生存储为 group2 等等。

我的结果是这样的

INSERT INTO r_test_group (student_id,job_id,group_name) values (22, 85,Group0 )
INSERT INTO r_test_group(student_id,job_id,group_name) values (23, 85,Group0 )
INSERT INTO r_test_group (student_id,job_id,group_name) values (25, 85,Group0 )
INSERT INTO r_test_group (student_id,job_id,group_name) values (26, 85,Group0)
INSERT INTO r_test_group (student_id,job_id,group_name) values (27, 85,Group0 )
INSERT INTO r_test_group(student_id,job_id,group_name) values (28, 85,Group0 )
INSERT INTO r_test_group (student_id,job_id,group_name) values (29, 85,Group0 )

Actual result what i am looking for is first 4 studets are group1 and second 4 group 2 and etc....

最佳答案

试试这个:

      $studentResults = $studentQuery->fetch_array();
$counter = 0;
for($j = 0; $j<$groups; $j++) {
for($i = 0; $i<4; $i++){
echo "INSERT INTO r_test_group
(student_id,job_id,group_name)
values (".$studentResults[$counter]['student_pid'].", ".$jobID.",Group".$j." )";
$counter++;
}
}

关于php - 按不同组sql将学生分开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41341744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com