php - Need help in Switch Statement setup -


$grade , $remark evaluated @ last value of $total (i.e ($check_ss=="ss")) want value @ every instance of if conditions. how do eg @ first condition gets value of grade , remark same subsequent conditions or haveto include switch statement inside each if statement

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

make switch statement function , use array hold grade , remark data. 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 can use within if() conditions, , grade @ 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 can dump out gradesalongtheway() find out grade @ each specific point. multi-dimensional array 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 can access grades @ each point index , grade, thussly:

echo $gradesalongtheway['en']['grade']; // produces letter check  echo $gradesalongtheway['ss']['remark']; //produces notation/message check 

you can loop on them.

foreach($gradesalongtheway $type => $gradearray) {     echo 'for check '. $type .' received an: '. $gradearray['grade'] .'. '. $gradearray['remark']; } 

edit

you're getting sql errors because of ' apostrophe in php code being injected mysql code. instead, need use mysqli prepared statements.

$stmt = mysqli_prepare($conn, "insert 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 make sure data correctly sanitized.

please read more mysqli prepared statements here


Comments