4Jean
4Jean

Reputation: 765

Need help in Switch Statement setup

$Grade and $Remark is always evaluated at the last value of $total (i.e where ($check_ss=="Ss")) but I want it to get value at every instance of the if conditions. How do I do Eg at first condition it gets value of grade and remark same for subsequent conditions or do i haveto include the switch statement inside each if statement

 //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";

Upvotes: 0

Views: 46

Answers (1)

Ohgodwhy
Ohgodwhy

Reputation: 50798

Make your switch statement into a function and also use an array to hold the grade and remark data. Something like this:

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

Now you can easily use this within your if() conditions, and get back the grade at each point.

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

Then you can dump out the gradesAlongTheWay() to find out what the grade was at each specific point. This will be a multi-dimensional array with 3 primary indices:

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

Now we can easily access the grades at each point by index and their grade, thussly:

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

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

You can also just loop over them.

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

Edit

You're getting SQL errors because of the ' apostrophe in your PHP code being injected into your MySQL code. Instead, you need to use 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);

This will make sure your data is correctly sanitized.

Please read more into mysqli prepared statements here

Upvotes: 1

Related Questions