Note that there are some explanatory texts on larger screens.

plurals
  1. POforeach() skipping first element in array
    text
    copied!<p>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:</p> <p>Here is the problem in an image: </p> <p><img src="https://i.stack.imgur.com/id7kF.png" alt="Error Scenario"></p> <p><img src="https://i.stack.imgur.com/S2AuE.png" alt="Correct Scenario"></p> <p>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()</p> <p>And in the second image the var_dump() is array(2) { [0]=> float(4) <a href="https://i.stack.imgur.com/id7kF.png" rel="nofollow noreferrer">1</a>=> float(3.5) }</p> <p>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()</p> <p>Here is my php code:</p> <pre><code>//$_POST['grades'] for the grades &lt;option&gt; and $_POST['types'] for the type (REG, HONORS) foreach(array_combine($_POST['grades'], $_POST['types']) as $code =&gt; $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; } } } </code></pre> <p>It might be the whole foreach() statement that needs reworking... I am up to writing the logic again if anyone says so...</p> <p>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. </p> <p>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 </p> <p>Thanks for the help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload