gpt4 book ai didi

php - sql中如何划分学生

转载 作者:太空宇宙 更新时间:2023-11-03 10:39:26 25 4
gpt4 key购买 nike

我想将学生分组并存储在数据库中

students 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15.....20

示例: Total no.of students 20

每组 4 名学生(1、2、3、4 组 1 和 5、6、7、8 组 2 等......)

那么应该创建了5个组

我试过这样我在 for 循环中犯的错误。

$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");

$totalIndividuals = mysqli_num_rows($studentQuery);
$groups = round($totalIndividuals/5,0,PHP_ROUND_HALF_DOWN);
for ($j = 1; $j <= $groups; $j++) {
for ($i = 0; $i <= $totalIndividuals; $i++) {
$GroupsQuery = $conn->query("INSERT INTO
r_test_group (student_id,job_id,group_name)
values ('".$studentResults['student_pid']."',
'".$jobID."','Group'".$j." )");
}
}

最佳答案

$stuData = array();
while($studentResults = $studentQuery->fetch_array()) {
$stuData[] = $studentResults;
}
$totalIndividuals = mysqli_num_rows($studentQuery);
$groups = round($totalIndividuals/$gd_individuals,0,PHP_ROUND_HALF_DOWN);
$count=0;
for($j = 0; $j<$groups; $j++) {
for($i =0; $i< $gd_individuals; $i++) {
if($stuData[$count]['student_pid']) {
echo "INSERT INTO r_test_group (student_id,job_id,group_name) values (".$stuData[$count]['student_pid'].", ".$jobID.",Group".$j." )";
$count++;
}
}
}
exit;

第一个forloop就是分组。第二个 forloop 是为学生除法。

关于php - sql中如何划分学生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41332768/

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