我正在尝试制作一个 GPA 计算器,允许用户根据需要输入更多字段。我将所有字段命名为“grades[]”,将所有字段命名为“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;
?>