You are here: Re: Strange results for foreach loop « All PHP « IT news, forums, messages
Re: Strange results for foreach loop

Posted by egi on 11/01/06 09:53

it's because the last value of $status is unchanged. you need to modify
the code to:

$question_count = 3;
$question_id = 2;

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

$status = '';

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

echo "q" . $number . $status . ".gif<br>";

stat_holyday@hotmail.com wrote:
> 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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация