在Switch Statement设置中需要帮助

时间:2016-03-26 17:03:34

标签: php

$ Grade和$ Remark总是在$ total的最后一个值(即where($ check_ss ==" Ss"))进行评估,但我希望它在if条件的每个实例中得到值。我该怎么办 例如,在第一个条件下,它获得了等级的值,并且对于后续条件也是相同 或者我必须在每个if语句中包含switch语句

 //Logic and Calc

if ($check_en=="En"){
    $ot_en = $ent1 + $ent2 + $ent3 + $ent4 + $enexm;
    $total = $ot_en;
}
if ($check_ms=="Ms"){
    $ot_ms = $mst1 + $mst2 + $mst3 + $mst4 + $msexm;
    $total = $ot_ms;
}
if ($check_ss=="Ss"){
    $ot_ss = $sst1 + $sst2 + $sst3 + $sst4 + $ssexm;
    $total = $ot_ss;
}
$ot= $ot_ms + $ot_ss + $ot_en;

  switch ($total) {

        case $total > 70:
            $grade = "A";
            $remark = "Excellent";
            break;
        case $total >= 60 && $total <= 69:
            $grade = "B";
            $remark = "Very Good";
            break;
        case $total >= 50 && $total <= 59:
            $grade = "C";
            $remark = "Good";
            break;
        case $total >= 45 && $total <= 49:
            $grade = "D";
            $remark = "Pass";
            break;
        case $total >= 40 && $total <= 44:
            $grade = "E";
            $remark = "Poor";
            break;
        case $total <= 39:
            $grade = "F";
            $remark = "Fail";
            break;
    }
    if ($total == 0) {
        $grade = "F";
        $remark = "Fail";

1 个答案:

答案 0 :(得分:1)

将switch语句转换为函数,并使用数组来保存成绩和备注数据。像这样:

function checkGrade($total)
{
    $ret = array();
    switch ($total) {

        case $total > 70:
            $ret['grade'] = "A";
            $ret['remark'] = "Excellent";
            break;
        case $total >= 60 && $total <= 69:
            $ret['grade'] = "B";
            $ret['remark'] = "Very Good";
            break;
        case $total >= 50 && $total <= 59:
            $ret['grade'] = "C";
            $ret['remark'] = "Good";
            break;
        case $total >= 45 && $total <= 49:
            $ret['grade'] = "D";
            $ret['remark'] = "Pass";
            break;
        case $total >= 40 && $total <= 44:
            $ret['grade'] = "E";
            $ret['remark'] = "Poor";
            break;
        case $total <= 39:
            $ret['grade'] = "F";
            $ret['remark'] = "Fail";
            break;
    }

    return $ret;
}

现在,您可以在if()条件下轻松使用此功能,并在每个点恢复成绩。

$gradesAlongTheWay = array();
if ($check_en=="En"){
    $ot_en = $ent1 + $ent2 + $ent3 + $ent4 + $enexm;
    $total = $ot_en;
    $gradesAlongTheWay['En'] = checkGrade($total);
}
if ($check_ms=="Ms"){
    $ot_ms = $mst1 + $mst2 + $mst3 + $mst4 + $msexm;
    $total = $ot_ms;
    $gradesAlongTheWay['Ms'] = checkGrade($total);
}
if ($check_ss=="Ss"){
    $ot_ss = $sst1 + $sst2 + $sst3 + $sst4 + $ssexm;
    $total = $ot_ss;
    $gradesAlongTheWay['Ss'] = checkGrade($total);
}

然后你可以转出gradesAlongTheWay()以找出每个特定点的成绩。这将是一个包含3个主要索引的多维数组:

Array(
    'En' => array(
        'grade' => 'Some Letter',
        'remark' => 'Some notation'
    ),
   'Ms' => array(
        'grade' => 'Some Letter',
        'remark' => 'Some notation'
    ),
   'Ss' => array(
        'grade' => 'Some Letter',
        'remark' => 'Some notation'
    )
)

现在,我们可以轻松地按索引及其等级访问每个点的成绩:

echo $gradesAlongTheWay['En']['grade']; // produces the letter from this check

echo $gradesAlongTheWay['Ss']['remark']; //produces the notation/message from this check

您也可以循环播放它们。

foreach($gradesAlongTheWay as $type => $gradeArray)
{
    echo 'For Check '. $type .' you received an: '. $gradeArray['grade'] .'. '. $gradeArray['remark'];
}

修改

由于PHP代码中的'撇号被注入到MySQL代码中,因此会出现SQL错误。相反,您需要使用mysqli prepared statements

$stmt = mysqli_prepare($conn, "INSERT INTO records VALUES (NULL, '?', 'English Language', '?', '?', '?', '?', '?', '?', ?, 'Poor'");

mysqli_stmt_bind_param($stmt, "ssssssss", $sid, $ent1, $ent2, $ent3, $ent4, $enexm, $ot_en, $gradesAlongTheWay['En']['grade']);

mysqli_stmt_execute($stmt);

这将确保您的数据被正确清理。

Please read more into mysqli prepared statements here