简体   繁体   English

使用php和mysql的评分系统

[英]Grading system using php and mysql

  1. In the database, some courses value are NULL, and I don't want to print courses that hava a NULL value. 在数据库中,某些课程的值为NULL,而我不想打印具有NULL值的课程。 I don't know how I can achieve this in php or mysql. 我不知道如何在php或mysql中实现。 I need something like so: 我需要这样的东西:

    if(course1!=NULL || course2!=NULL || course3!=NULL || course4!=NULL){ // echo course1; echo course2; echo course3; echo course4; }

The database looks like this: 该数据库如下所示:

数据库

And the output so far look like this: 到目前为止的输出如下所示: 在此处输入图片说明

  1. A remark() function which check whether the course is A, B, C, D, E, or F is not working properly. 检查课程是否为A,B,C,D,E或F的remark()函数无法正常工作。 I want the remark() function to output either Excellent, Credit, Pass, or fail base on the grade. 我希望remark()函数根据成绩输出优秀,学分,及格或不及格。

    Follows is the code: 以下是代码:

     function remark(){ global $mathematics, $physics, $chemistry, $lang; if($mathematics=='A' || $physics=='A' || $chemistry=='A' || $lang=='A'){ $remark = "Excellent"; } elseif($mathematics=='B' || $physics=='B' || $chemistry=='B' || $lang=='B' || $mathematics=='C' || $physics=='C' || $chemistry=='C' || $lang=='C'){ $remark = "Credit"; } elseif($mathematics=='D' || $physics=='D' || $chemistry=='D' || $lang=='D' || $mathematics=='E' || $physics=='E' || $chemistry=='E' || $lang=='E'){ $remark = "Pass"; } else{ $remark = "Fail"; } return $remark; } if(isset($_POST['student_id']) && !empty($_POST['student_id'])){$student_id = $_POST['student_id']; $check = "SELECT * FROM sample1 WHERE student_id='$student_id'"; $check_query = mysqli_query($connection, $check); if(mysqli_num_rows($check_query)>0){ while($query_row = mysqli_fetch_assoc($check_query)){ $mathematics = $query_row['mathematics']; $physics = $query_row['physics']; $chemistry = $query_row['chm']; $lang = $query_row['lang']; echo "<table border=1 width=50%> <tr> <td>Name: ".$query_row['name']." </td> <td>Student Id: ".$query_row['student_id']."</td> <td>Level: ".$query_row['level']."</td> </tr> <tr> <th> Course </th> <th> Grade </th> <th> Remark </th> </tr> <tr> <td>Mathematics </td> <td>".$mathematics." </td></td> <td>".remark()." </td></tr> <tr> <td>Physics </td> <td>".$physics."</td> </td><td>".remark()." </td></tr> <tr> <td>Chemistry </td> <td>".$chemistry."</td> </td><td>".remark()." </td></tr> <tr> <td>Language </td> <td>".$lang."</td> </td><td>".remark()." </td></tr> </table>"; } } else{echo "Record not found";} } 

About the MYSQL not null values, you use this: 关于MYSQL不是空值,您可以使用以下命令:

SELECT * FROM yourtable WHERE course1 IS NOT NULL

So, IS NOT NULL will give you queries where that column (course1) is not null. 因此,“ IS NOT NULL”将为您查询该列(course1)不为空的地方。 Here more info. 这里更多信息。

And for PHP, your remark() function has a wrong approach. 对于PHP,您的remark()函数使用了错误的方法。 I will give you one that is correct for your purpouse. 我会给你一个正确的目的。

function remark($course){

    switch($course) {

        case 'A':
            return 'Excellent';
        break;

        case 'B':
        case 'C':
            return 'Credit';
        break;

        case 'D':
        case 'E':
            return 'Pass';
        break;

        default:
            return 'Fail';
        break;

    }

}

So in your HTML table you do this for each course: 因此,在您的HTML表格中,您可以针对每门课程进行此操作:

while ($query_row = mysqli_fetch_assoc($check_query)) {

    $mathematics = $query_row['mathematics'];
    $physics     = $query_row['physics'];
    $chemistry   = $query_row['chm'];
    $lang        = $query_row['lang'];

    echo "<table border=1 width=50%>
        <tr> <td>Name: " . $query_row['name'] . "  </td>
            <td>Student Id: " . $query_row['student_id'] . "</td>
            <td>Level: " . $query_row['level'] . "</td>
        </tr>
        <tr>
            <th> Course </th>
            <th> Grade </th>
            <th> Remark </th>
        </tr>"

        if(!is_null($mathematics)) echo "<tr> <td>Mathematics </td> <td>" . $mathematics . " </td></td> <td>" . remark($mathematics) . " </td></tr>";
        if(!is_null($physics)) echo "<tr> <td>Physics </td> <td>" . $physics . "</td> </td><td>" . remark($physics) . " </td></tr>";
        if(!is_null($chemistry)) echo "<tr> <td>Chemistry </td> <td>" . $chemistry . "</td> </td><td>" . remark($chemistry) . " </td></tr>";
        if(!is_null($lang)) echo "<tr> <td>Language </td> <td>" . $lang . "</td> </td><td>" . remark($lang) . " </td></tr>";

    echo "</table>";
}

Quick answer for the first issue extending the answer of @P0IT10n (?): 快速扩展第一个问题的答案@ P0IT10n(?):

 <?php
     if(!is_null($mathematics)) { 
 ?>
        <tr> <td>Mathematics </td> <td>".$mathematics." </td></td> <td>".remark($mathematics)." </td></tr>
 <?php
     }
 ?>
 // repeat for other courses

BUT
rethink your database structure. 重新考虑您的数据库结构。
Once you have another course you're f***... 一旦您再修一门课程,您就会感到...
Courses should have their own table, results another one. 课程应该有自己的表格,结果又一张。
Read about database normalisation! 了解有关数据库规范化的信息!

EDIT 编辑
Another extension: 另一个扩展:
It would be even easier if you'd have an array like this: 如果您有一个像这样的数组,它将更加容易:

$remarks = array('A'=>'Excellent', 'B'=>'Good',...);

then you need no extra function with a switch, all you need to do is: 那么您不需要使用开关的额外功能,您所需要做的就是:

echo $remarks[$mathematics]; // -> Excellent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM