2016-03-26 46 views
0

$ Grade和$ Remark總是以$ total的最後一個值(即where($ check_ss ==「Ss」))來計算,但我希望它能在每個if條件。我該怎麼做 例如,在第一種情況它會檔次和言論的價值同爲後續條件 還是我haveto包括內部的每個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

讓你的switch語句進入一個函數的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' 
    ) 
) 

現在,我們可以很容易地通過索引來訪問每個點上的成績和他們的等級,thussly:

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代碼'撇號的SQL錯誤被注入到你的MySQL的代碼。相反,您需要使用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

+0

'$ SQL1 =「INSERT INTO'records' VALUES(NULL, '$ SID', '英語', '$ ENT1', '$ ENT2', '$ ent3',「$ ent4 ','$ enexm','$ ot_en','$ gradesAlongTheWay ['En'] ['grade']','Poor')「; if(mysqli_query($ conn,$ sql1)){ echo「Values Inserted」; } else { echo「Failed」.mysqli_error($ conn); }' – 4Jean

+0

嘗試插入數據庫時​​出現錯誤''grades gradesAlongTheWay ['En'] ['grade']' – 4Jean

+0

它是否符合該條件?否則,該索引不會被設置。 – Ohgodwhy