Strange results for foreach loop



Greetings. I'm confused. I'm attemting to create dynamic buttons based
on the count of questions in a database. The button has 3 states,
blank, selected, and inactive. The script below works great, but with a
strange bug that seems impossible to me.

When the $question_count and $question_id are 2 or more values apart
the script works, but when they are only 1 value apart, it bugs out.

Examples...

--
$question_count = "5";
$question_id = "2";

Output:
q1.gif
q2_selected.gif
q3.gif
q4.gif
q5.gif

--
$question_count = "4";
$question_id = "2";

Output:
q1.gif
q2_selected.gif
q3.gif
q4.gif
q5_blank.gif

--
$question_count = "4";
$question_id = "4";

Output:
q1.gif
q2.gif
q3.gif
q4_selected.gif
q5_blank.gif

--
$question_count = "4";
$question_id = "3";

Output:
q1.gif
q2.gif
q3_selected.gif
q4_selected.gif <--- WTF
q5_blank.gif

--
End examples

It's weird. All permutations work properly unless they're 1 apart from
each other... 1 to 2, 2 to 3, 3 to 4, 4 to 5...

Any ideas? What am I missing?

Here's the code:

$question_count = "3";
$question_id = "2";

foreach (range(1, 5) as $number) {

if ($question_id == $number) {
$status = "_selected";
}

elseif ($question_count < $number) {
$status = "_blank";
}

elseif ($question_count > $number) {
$status = "";
}
echo "q" . $number . $status . ".gif<br>";

}

Thanks for you attention,
Luc M. Forget

.


Quantcast