gpt4 book ai didi

php - php mysql中的计算

转载 作者:行者123 更新时间:2023-11-28 23:46:00 25 4
gpt4 key购买 nike

我想计算每个学生的出勤率,它会显示属于每个学生的每个出勤率。下面是我的代码:

<tbody>
<?php
$data = "SELECT SUM(studAtt_endTime - studAtt_startTime) FROM studentAttendance";
$result = $conn->query($data)
or die ("Error: ".mysqli_error($conn)); //look if theres any error
while($ser2=mysqli_fetch_array($result)) {
?>

<?php
$counter = 1;
$data = "SELECT * FROM student INNER JOIN studentAttendance ON student.stud_matric=studentAttendance.student_stud_matric
INNER JOIN course ON course.course_code=studentAttendance.course_course_code
WHERE course_course_code LIKE '%$_GET[course]%'
GROUP BY student_stud_matric";
$result = $conn->query($data)
or die ("Error: ".mysqli_error($conn)); //look if theres any error
while($ser=mysqli_fetch_array($result)) {
?>
<tr>
<td><center><?php echo $counter;
$counter++; ?></center></td>
<td><?php echo $ser["stud_matric"];?></td>
<td><?php echo $ser["stud_name"];?></td>
<td><?php
$a = $ser["course_contacthour"];
$b = "14";
$tch = ($a * 2 ) * $b; // tch= total contact hour in 14 week
echo number_format($ser2["SUM(studAtt_endTime - studAtt_startTime)"] / $tch * 100, 2);?></td>
</tr>
<?php
}
?>
<?php
}
?>
</tbody>

问题是,此代码将汇总所有学生的出勤百分比,并且它将显示每个学生的相同百分比,而不是学生本身的百分比。

最佳答案

<html>
<head>
<title></title>
</head>
<body>
<table>
<tbody>
<?php

$counter = 1;

/* Some rudimentary filtering at the very least, better use prepared statements or PDO */
$course = addslashes( filter_input( INPUT_GET,'course',FILTER_SANITIZE_STRING ) );

/* I think you can combine the two sql queries and have only 1 loop */
$data = "select *,
( select sum( `studatt_endtime` - `studatt_starttime` ) from `studentattendance` ) as 'sum'
from `student` s
inner join `studentattendance` a on s.`stud_matric`=a.`student_stud_matric`
inner join `course` c on c.`course_code`=a.`course_course_code`
where a.`course_course_code` like '%".$course."%'
group by a.`student_stud_matric`;";

/* Query the db - do not reveal too much information if there is a problem */
$result = $conn->query( $data ) or die ("Error: Something went wrong...");
if( $result ){

/* Loop through recordset */
while( $ser=$result->fetch_object() ) {

/* Do your calculations */
$a = $ser->course_contacthour;
$b = "14";
$tch = ( $a * 2 ) * $b;
$tot=number_format( $ser->sum / $tch * 100, 2);

/* write the output */
echo '
<tr>
<td>
<center>'.$counter.'</center>
</td>
<td>'.$ser->stud_matric.'</td>
<td>'.$ser->stud_name.'</td>
<td>'.$tot.'</td>
</tr>';

$counter++;
}
}
@mysqli_free_result( $result );
?>
</tbody>
</table>
</body>
</html>

关于php - php mysql中的计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33563056/

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