1

http://fog.ccsf.cc.ca.us/~tboegel/semesterGPA1.php

我的表格应该计算课程的 GPA(上面的链接)。我仍然需要计算总分、GPA、总质量。我想我可以自己弄清楚,但我需要找出如何 $_REQUEST['course1'], course2, course3, units1, units2, etc...(见下文)

到目前为止,我有

<?php
$course = $_REQUEST['course0'];
$units = $_REQUEST['units0'];
$letterGrade = $_REQUEST['letterGrade0'];
$letterGrade = strtoupper($letterGrade);
if($letterGrade == 'A') {
    $numberGrade = 4;
} elseif ($letterGrade == 'B') {
    $numberGrade = 3;                                                                           
} elseif ($letterGrade == 'C')  {
    $numberGrade = 2;                                                                           
} elseif ($letterGrade == 'D')  {
    $numberGrade = 1;                                                                           
} else {
    $numberGrade = 0;                                                                           
}

$qualityPoints = $units * $numberGrade;

function calculateQualityPoints($course, $units, $letterGrade, $qualityPoints) {
echo "<tr><td>$course</td><td>$units</td><td>$letterGrade</td><td>$qualityPoints</td></tr>";

}

echo "<table width='50%' align='left'><tr><th>Course</th><th>Units</th><th>Letter Grade</th><th>Quality Points</th></tr>";
calculateQualityPoints($course, $units, $letterGrade, $qualityPoints);                          
echo "<tr><td><strong>Total</strong></td><td><strong>total</strong></td><td></td><td><strong>quality total</strong></td></tr><tr><td><strong>GPA</strong></td><td><strong>GPA #</strong></td></tr></table>";                                                                                    

?>

只有第一个文本框有效。如何获取 course1、course2、course3、units1、units2、letterGrade1 等?

此代码来自 HTML 表单,用于输入课程、年级和单元。

<?php
  for ($i=0; $i<5; $i+=1) {
    print "<tr>\n";
    print "\t<td><input type='text' name='course$i'></td>\n";
    print "\t<td><input type='text' name='units$i' size=5></td>\n";
    print "\t<td><input type='text' name='letterGrade$i' size=5></td>\n";
    print "</tr>\n";
  }
  ?>
4

4 回答 4

1

Your using the post method in your form, so in semesterGPA2.php, you should use $POST['name'] to access those input variables, where name is Course 0, 1, 2, etc. You can use another for loop to display this.

Your code should look something like this, hope it helps. Don't guarantee there are no typos!.

<?php
//maybe put the th tag here, course, units lettergrade, etc.
$qualityPoints = 0;
$units = 0;
for($i=0; $i<5; $i++) {

    //these are just the names in input name = "", can rename yo anything you want
    $courseName = 'course'.$i; 
    $unitsName = 'units'.$i;
    $letterGradeName = 'lettergrade'.$i;

    //we are using post to retrieve these form input variables
    $letterGrade = $POST[$letterGradename];
    $units = $POST[$unitsName];
    $course = $POST[$courseName];
    //calculate the quality points for each one
    $qualityPoints = calculateQualityPoints($units, $letterGrade);
    //maybe hereyou can just output(echo) each row with the above information
    //echo above info

    //you can aggregate them here for output of the final grade, like a report card
    $qualityPoints += $qualityPoints;
    $units += $units;
}

//here you can use the total quality points and the total units to calculate gpa
$GPA = ($qualityPoints/$units)/25;
echo "GPA:". $GPA;
//or even make a function to calculate GPA
//Why not even create a report card class that encapsulates all of this, but may be over kill!
function calculateQualityPoints($units, $letterGrade) {
    if($letterGrade == 'A') {
        //you can conver the letter grade to number grade, or you can just do it directly 
        $qualityPoints = 100 * $units;
    } elseif ($letterGrade == 'B') {
        $qualityPoints = 75 * $units;                                                                         
    } elseif ($letterGrade == 'C')  {
        $qualityPoints = 50 * $units;                                                                        
    } elseif ($letterGrade == 'D')  {
        $qualityPoints = 25 * $units;                                                                           
    } else {
        $qualityPoints = 0;                                                                          
    }
    return $qualityPoints;
}

?>
于 2012-10-13T20:59:47.967 回答
1

只需检查一下,如果值 == '' 或 NULL

于 2012-10-13T21:28:01.190 回答
0

用这个替换calculateQualityPoints($course, $units, $letterGrade, $qualityPoints); ......它应该工作

for ($i=0; $i<5; $i+=1) {
   echo "<tr><td>".$_REQUEST['course'.$i]."</td><td>".$_REQUEST['units'.$i]."</td>";      
   echo "<td>$letterGrade</td><td>$qualityPoints</td></tr>";
}
于 2012-10-13T20:13:29.577 回答
0
<?php
    $ban1 = 50;
    $ban2 = -22;
?>

<!DOCTYPE html>
<html>
    <head>
        <title>Student Markssheet</title>
        <style>
            th {
                font-size: 20px;
                padding: 12px;
                background-color: blueviolet;
                color: #fff;
            }
            td{
                padding: 8px;
                font-size: 18px;
            }
        </style>
    </head>
    <body>
        <center><h2>Enter Your Data</h2></center>
            <table width="100%">
               <tr>
                   <th width="5%">SL NO.</th>
                   <th width="20%">Subject Name</th>
                   <th width="5%">Marks</th>
                   <th width="5%">L.G.</th>
                   <th width="5%">GP</th>
               </tr>
               <tr> <!--Bangla First Paper-->
                   <td>01</td>
                   <td>Bangla First Paper</td>
                   <td><?php echo $ban1 ?></td>
                   <td>
                       <?php
                          if(101 > $ban1 | $ban1 > 79){echo 'A+';}   
                          elseif(80 > $ban1 || $ban1 > 69){echo 'A';}   
                          elseif(70 > $ban1 || $ban1 > 59){echo 'A-';}  
                          elseif(60 > $ban1 || $ban1 > 49){echo 'B';}   
                          elseif(50 > $ban1 || $ban1 > 39){echo 'C';}   
                          elseif(40 > $ban1 || $ban1 > 32){echo 'D';}
                          elseif(33 > $ban1 || $ban1 > 0){echo 'F';}
                          else{ echo "Unalocated Number";} 
                       ?>
                   </td>
                   <td>
                       <?php
                          if(101 > $ban1 || $ban1 > 79){echo '5.00';}   
                          elseif(80 > $ban1 || $ban1 > 69){echo '4.00';}   
                          elseif(70 > $ban1 || $ban1 > 59){echo '3.50';}   
                          elseif(60 > $ban1 || $ban1 > 49){echo '3.00';}   
                          elseif(50 > $ban1 || $ban1 > 39){echo '2.00';}   
                          elseif(40 > $ban1 || $ban1 > 32){echo '1.00';}   
                          elseif(33 > $ban1 || $ban1 > 0){echo '0.00';}
                          else{ echo "Unalocated Number";} 
                       ?>
                   </td>
               </tr>
               <tr> <!--Bangla Secound Paper-->
                   <td>02</td>
                   <td>Bangla Secound Paper</td>
                   <td><?php echo $ban2 ?></td>
                   <td>
                       <?php
                          if(101 > $ban1 || $ban1 > 79){echo 'A+';}  
                          else if(80 > $ban2 || $ban2 > 69){echo 'A';}   
                          else if(70 > $ban2 || $ban2 > 59){echo 'A-';}   
                          else if(60 > $ban2 || $ban2 > 49){echo 'B';}   
                          else if(50 > $ban2 || $ban2 > 39){echo 'C';}   
                          else if(40 > $ban2 || $ban2 > 32){echo 'D';}   
                          else if(33 > $ban2 || $ban2 > 0){echo 'F';}
                          else{ echo "Unalocated Number";} 
                       ?>
                   </td>
                   <td>
                       <?php
                          if(101 > $ban1 || $ban1 > 79){echo '5.00';}   
                          else if(80 > $ban2 || $ban2 > 69){echo '4.00';}   
                          else if(70 > $ban2 || $ban2 > 59){echo '3.50';}   
                          else if(60 > $ban2 || $ban2 > 49){echo '3.00';}   
                          else if(50 > $ban2 || $ban2 > 39){echo '2.00';}   
                          else if(40 > $ban2 || $ban2 > 32){echo '1.00';}   
                          else if(33 > $ban2 || $ban2 > 0){echo '0.00';}
                          else{ echo "Unalocated Number";} 
                       ?>
                   </td>
               </tr>
               
            </table>
    </body>
</html>
于 2021-08-24T16:26:55.867 回答