2017-08-09 16 views
0

このシナリオでは、平均マーク、最低マークまたは最高マークを計算する学生マークの表を作成しました。これは、すべての作業罰金だが、私はこのような特定のデータに応じて等級を計算し、1つのより多くのことを行う必要があります。どのように私はPHPのテーブルで格付けを与えることができますか?

if marks is 85% and above, give grade A 
if marks is 60% and above, give grade B 
if marks is 55% and above, give grade C 
if marks is 54% and less, mark them as Fail, 
<!DOCTYPE html> 
<html> 

<head> 
    <title>Assignment 1</title> 
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" /> 
</head> 

<body> 
    <div class="container"> 
    <table class="table table-striped"> 
     <thead> 
     <tr> 
      <th>Name</th> 
      <th>Physics</th> 
      <th>Maths</th> 
      <th>Chemistry</th> 
      <th>Lowest Marks</th> 
      <th>Highest Marks</th> 
     </tr> 
     </thead> 
     <tbody> 
<?php 
$marks = array( 
      "mohammad" => array(
           "physics" => 35,   
           "maths" => 30,  
           "chemistry" => 39  
          ), 
      "qadir" => array(
           "physics" => 30, 
           "maths" => 32, 
           "chemistry" => 29 
          ), 
      "zara" => array(
           "physics" => 31, 
           "maths" => 22, 
           "chemistry" => 39 
          ) 
      );     
$average=0; 
$lowest=0; 
$greatest=0; 
$physics=0; 
$chemistry=0; 
$maths=0; 
$arr=Array(0); 
$count=0; 
foreach($marks as $row => $innerArray){ 
    $average=0; 
    $lowest=0; 
    $greatest=0; 
    $count=0; 
    echo "<tr>"; 
    echo "<td>$row</td>"; 
    foreach($innerArray as $innerRow => $value){         
     if($value>$greatest){ 
      $greatest=$value; 
     }else{ 
      $lowest=$value; 
     } 
     echo "<td>$value</td>"; 
     $arr[$count]+=$value; 
     $count++;        
    } 
    $average=round($average/3,2);       
    echo "<td>$lowest</td>"; 
    echo "<td>$greatest</td>"; 
    echo "</tr>"; 
} 
echo "<tr><td>Average Marks</td><td>".round($arr[0]/3,2)."</td><td>".round($arr[1]/3,2)."</td><td>".round($arr[2]/3,2)."</td></tr>"; 
?> 
     </tbody> 
    </table> 
    </div> 
</body> 

</html> 
+3

単純な 'if-else'ステートメントはどうでしょうか? ô.o – Twinfriends

答えて

2

コードの下に試してみてください、

<?php 
 
$marks = $average; 
 

 
if ($marks > 85) { 
 
    echo "Grade A"; 
 
} else if ($marks > 60) { 
 
    echo "Grade B"; 
 
} else if ($marks > 55) { 
 
    echo "Grade C"; 
 
} 
 
else{ 
 
    echo "Fail"; 
 
} 
 
?>

1

TRUEの場合、switch/caseを使用できます。

switch (TRUE) { 

case ($marks > 84) : $grade = 'A'; break; 
case ($marks > 59) : $grade = 'B'; break; 
case ($marks > 54) : $grade = 'C'; break; 
default : $grade = 'Fail'; 

} 
関連する問題