我正在尝试制作GPA计算器,允许用户根据需要输入更多字段。我将所有字段命名为'grade []',将所有字段命名为'units []'。提交数据时,php文件会扫描每个字段,并将数据分别输入名为$ GradeArray和$ UnitsArray的变量中,然后对这两个变量进行所需的所有计算。
我让计算器工作正常,但后来我测试看是否有任何错误存在。当我跳过任何字段并将数据输入下一个字段时,结果将变为0,或者只是错误的答案。
这是一张错误的照片:
但是当我按顺序进入字段时,没有遗漏任何字段,答案就出来了..
我猜我的foreach()语句存在问题。无论如何这里是代码......希望你能帮忙!
Jquery动态添加额外的输入字段(为可读性增加了空格):
$('#add').click(function() {
$('<div>
<select name = "grades[]">
<option value = "0"> SELECT </option>
<option value = "A"> A</option>
<option value = "AMINUS"> A- </option>
<option value = "BPLUS"> B+ </option>
<option value = "B"> B </option>
<option value = "BMINUS"> B- </option>
<option value = "CPLUS"> C+ </option>
<option value = "C"> C </option>
<option value = "CMINUS"> C- </option>
<option value = "DPLUS"> D+ </option>
<option value ="D"> D </option>
<option value = "DMINUS"> D-</option>
<option value = "F"> F </option>
</select> <input name = "units[]" />
</div> ').fadeIn('slow').appendTo('.inputs');
i++;
});
HTML:
<form method = "post" action = "process.php" name = "myform" id = "myform">
<div class = "inputs">
<select name = "grades[]">
<div><option value = "0"> SELECT </option></div>
<div><option value = "A" > A </option></div>
<div><option value = "AMINUS"> A-</option></div>
<div><option value = "BPLUS" > B+ </option></div>
<div><option value = "B"> B </option></div>
<div><option value = "BMINUS" > B- </option></div>
<div><option value = "CPLUS"> C+ </option></div>
<div><option value = "C" > C </option></div>
<div><option value = "CMINUS"> C- </option></div>
<div><option value = "DPLUS" > D+ </option></div>
<div><option value = "D"> D </option></div>
<div><option value = "DMINUS" > D- </option></div>
<div><option value = "F"> F </option> </div>
</select>
<input name = "units[]" type = "text"/>
<br>
<select name = "grades[]">
<div><option value = "0"> SELECT </option></div>
<div><option value = "A" > A </option></div>
<div><option value = "AMINUS"> A-</option></div>
<div><option value = "BPLUS" > B+ </option></div>
<div><option value = "B"> B </option></div>
<div><option value = "BMINUS" > B- </option></div>
<div><option value = "CPLUS"> C+ </option></div>
<div><option value = "C" > C </option></div>
<div><option value = "CMINUS"> C- </option></div>
<div><option value = "DPLUS" > D+ </option></div>
<div><option value = "D"> D </option></div>
<div><option value = "DMINUS" > D- </option></div>
<div><option value = "F"> F </option> </div>
</select>
<input name = "units[]" type = "text" />
<br>
<select name = "grades[]">
<div><option value = "0"> SELECT </option></div>
<div><option value = "A" > A </option></div>
<div><option value = "AMINUS"> A-</option></div>
<div><option value = "BPLUS" > B+ </option></div>
<div><option value = "B"> B </option></div>
<div><option value = "BMINUS" > B- </option></div>
<div><option value = "CPLUS"> C+ </option></div>
<div><option value = "C" > C </option></div>
<div><option value = "CMINUS"> C- </option></div>
<div><option value = "DPLUS" > D+ </option></div>
<div><option value = "D"> D </option></div>
<div><option value = "DMINUS" > D- </option></div>
<div><option value = "F"> F </option> </div>
</select>
<input name = "units[]" type = "text" />
<br>
<select name = "grades[]">
<div><option value = "0"> SELECT </option></div>
<div><option value = "A" > A </option></div>
<div><option value = "AMINUS"> A-</option></div>
<div><option value = "BPLUS" > B+ </option></div>
<div><option value = "B"> B </option></div>
<div><option value = "BMINUS" > B- </option></div>
<div><option value = "CPLUS"> C+ </option></div>
<div><option value = "C" > C </option></div>
<div><option value = "CMINUS"> C- </option></div>
<div><option value = "DPLUS" > D+ </option></div>
<div><option value = "D"> D </option></div>
<div><option value = "DMINUS" > D- </option></div>
<div><option value = "F"> F </option> </div>
</select>
<input name = "units[]" type = "text"/>
</div>
<input type = "submit" value = 'Submit' id = "Submit" style = "font-family:'HelveticaNeue-Light', 'Helvetica Neue Light', 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; width: 80px;background-color: #c5Da37; border: none; color: white;"/>
<input type ="reset" style = "background-color: #0499ff; width: 90px; color: white;">
</form>
PHP:
//ARRAYS
$GradeArray = array();
$UnitArray = array();
$GradePoints = array();
//INPUT GRADES (NUMERICAL) INTO GRADE ARRAY
foreach($_POST['grades'] as $grades)
{
if ($grades == "A"){
$GradeArray[] = 4.0;
}
else if ($grades == "AMINUS"){
$GradeArray[] = 3.7;
}
else if ($grades == "BPLUS"){
$GradeArray[] = 3.3;
}
else if ($grades == "B"){
$GradeArray[] = 3.0;
}
else if ($grades == "BMINUS"){
$GradeArray[] = 2.7;
}
else if ($grades == "CPLUS"){
$GradeArray[] = 2.3;
}
else if ($grades == "C"){
$GradeArray[] = 3.0;
}
else if ($grades == "CMINUS"){
$GradeArray[] = 2.7;
}
else if ($grades == "DPLUS"){
$GradeArray[] = 2.3;
}
else if ($grades == "D"){
$GradeArray[] = 2.0;
}
else if ($grades == "DMINUS"){
$GradeArray[] = 1.7;
}
else if ($grades == "F"){
$GradeArray[] = 0;
}
}
//INSERT VALUES FROM UNITS INTO UNITS ARRAY
foreach($_POST['units'] as $units)
{
if ($units == NULL)
{
$UnitArray[] = 0;
}
$UnitArray[] = $units;
}
//CALCULATE GP
for ($i = 0; $i <= count($GradeArray); $i++)
{
$GradePoints[$i] = $GradeArray[$i] * $UnitArray[$i];
}
//SUM UP GRADE POINTS
$sumGradePoints= 0;
for($i = 0; $i != count($GradeArray); $i++)
{
$currentItem = $GradePoints[$i];
$sum = $sumGradePoints+$currentItem;
$sumGradePoints = $sum;
}
//SUM UP CREDIT HOURS
$sumUnitPoints = 0;
for ($i = 0; $i <= count($UnitArray); $i++)
{
$currentItem = $UnitArray[$i];
$sum = $sumUnitPoints+$currentItem;
$sumUnitPoints = $sum;
}
$result = $sumGradePoints/$sumUnitPoints;
echo $result;
?>