【问题标题】:calculation in php mysql在 php mysql 中计算
【发布时间】:2015-11-06 09:06:02
【问题描述】:

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

<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>

问题是,这段代码会总结所有学生的出勤率,并且会为每个学生显示相同的百分比,而不是学生本身的百分比。

【问题讨论】:

  • 您有一个使用 $result 的嵌套 sl 查询 - 外部查询也使用它,这可能会导致问题。
  • 还有其他问题。为什么要两个查询?您在第二个查询中错过了参数名称的引号(即:'%$_GET[course]%' 应该是 '%{$_GET['course']}%',而 number_format($ser2["SUM(studAtt_endTime - studAtt_startTime)"] / $tch * 100, 2) 是什么??
  • 我有两个查询,因为它不能仅由一个查询完成,或者我无法编写正确的查询。 number_format($ser2["SUM(studAtt_endTime - studAtt_startTime)"] / $tch * 100, 2) 是学生出勤率的计算。因此,我需要第一个查询中的SUM(studAtt_endTime - studAtt_startTime)。但是,显示的结果是根据所有学生的总“SUM(studAtt_endTime - studAtt_startTime)”计算的,而不是针对我想要的每个特定学生。 @RamRaider

标签: php mysql sql sum wamp


【解决方案1】:
<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>

【讨论】:

  • 是的,这段代码我更有意义 tq 这么多!但我仍然无法正确计算。看看我在@RamRaider 上方编辑过的帖子中得到了什么
【解决方案2】:

与其编辑上面/下面的内容并混淆事物,我将在这里发布一些其他内容。在没有看到架构的情况下,我不确定以下 SQL 是否正确,因为我不知道 studatt_endtimestudatt_starttime 列是否在 studentAttendance 表或 student 表中,因此您可能需要更改使用的前缀:

/* Now the `sum` is pertinent to the student whereas before it was not */
$data = "select *,
        sum( a.`studatt_endtime` - a.`studatt_starttime` ) 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`;";

另外,我不知道你到底想计算什么,数学对我来说从来都不是强项,但计算似乎有点模棱两可,因为没有遵守 BODMAS 计算方法,其中 @987654328 @ 代表 Brackets, Order, Division, Multiplication, Addition, Subtraction - see here for details,因此可以用不同的方式解释计算,例如:

$tot=number_format( $ser->sum / ( $tch * 100 ), 2 );
$alt_tot=number_format( ( $ser->sum / $tch ) * 100, 2 );

【讨论】:

    猜你喜欢
    • 2011-06-18
    • 1970-01-01
    • 2023-03-09
    • 2011-09-10
    • 2021-04-27
    • 2011-01-02
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多