2012-10-13 66 views
1

http://fog.ccsf.cc.ca.us/~tboegel/semesterGPA1.phpPHP計算GPA和用戶定義的函數

我的形式應該計算課程的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"; 
    } 
    ?> 
+0

你可以有'名字= 「當然[]」'和'$ _REQUEST [ '當然']'將是一個陣列,afaicr .. – MiniGod

回答

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>"; 
} 
+0

這似乎可以工作。謝謝,但我需要使用用戶定義的函數,因爲我的任務需要我使用函數 – yan

1

你使用表單POST方法,所以在semesterGPA2.php,你應該使用$ POST [ '名']到訪問這些輸入變量,其中名稱是課程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; 
} 

?> 
+0

我忘了添加文本字段可以留空,所以如果我留下一個文本字段爲空。我想我得到一個使用該循環的錯誤 – yan

+0

只需添加一個檢查來查看帖子值是什麼,如果黑色或NULL,並相應地重新路由您的代碼 –

1

只是做一個檢查,如果該值== '' 或爲空