PHP计算GPA和用户定义的函数

时间:2012-10-13 20:00:53

标签: php

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

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

到目前为止,我有

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

3 个答案:

答案 0 :(得分:1)

你在表单中使用post方法,所以在semesterGPA2.php中,你应该使用$ POST ['name']来访问那些输入变量,其中name是Course 0,1,2等。你可以使用另一个for循环显示这个。

您的代码看起来应该是这样的,希望它有所帮助。不保证没有拼写错误!

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

?>

答案 1 :(得分:1)

如果值==''或NULL

,只需检查一下

答案 2 :(得分: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>";
}