Trying to create a GPA Calculator. I have a form that submits to a php file and then stores all data from the form in a php array(). The calculator works great until I enter the same value. I think this wont make sense until I show some pictures:
Here is the problem in an image:


So in the first image I enter a A and then another A which outputs array(1) { [0]=> float(4.5) } when using var_dump()
And in the second image the var_dump() is array(2) { [0]=> float(4) 1=> float(3.5) }
It is skipping the first row in the first image… just in case A is supposed to equal 4.0 in REG and 4.5 in HONORS. It might be the array_combine()
Here is my php code:
//$_POST['grades'] for the grades <option> and $_POST['types'] for the type (REG, HONORS)
foreach(array_combine($_POST['grades'], $_POST['types']) as $code => $count)
{
if ($code == "A")
{
if ($count == "REGULAR")
{
$GradeArray[] = 4.0;
}
else if ($count == "HONORS")
{
$GradeArray[] = 4.5;
}
else if ($count == "COLLEGE")
{
$GradeArray[] = 5.0;
}
}
else if ($code == "B")
{
if ($count == "REGULAR")
{
$GradeArray[] = 3.0;
}
else if ($count == "HONORS")
{
$GradeArray[] = 3.5;
}
else if ($count == "COLLEGE")
{
$GradeArray[] = 4.0;
}
}
else if ($code == "C")
{
if ($count == "REGULAR")
{
$GradeArray[] = 2.0;
}
else if ($count == "HONORS")
{
$GradeArray[] = 2.5;
}
else if ($count == "COLLEGE")
{
$GradeArray[] = 3.0;
}
}
else if ($code == "D")
{
if ($count == "REGULAR")
{
$GradeArray[] = 1.0;
}
else if ($count == "HONORS")
{
$GradeArray[] = 1.5;
}
else if ($count == "COLLEGE")
{
$GradeArray[] = 2.0;
}
}
else if ($code == "F")
{
if ($count == "REGULAR")
{
$GradeArray[] = 0.0;
}
else if ($count == "HONORS")
{
$GradeArray[] = .5;
}
else if ($count == "COLLEGE")
{
$GradeArray[] = 1.0;
}
}
}
It might be the whole foreach() statement that needs reworking… I am up to writing the logic again if anyone says so…
I don’t want to clog up the question with code so if you absolutely need the html just ask and I will add in an edit.
EDIT: I am also thinking I need to rewrite the logic… I have never used array_combine() before… I just need to make sure the corresponds with the related
Thanks for the help!
look at this array and output look the VALUES “a” which becomes the key of the resultant array,
so if u want the full array to be combined with the key of first array then first array must kave unique values.
solution (may not be the best )