473,327 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Strange results from 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

Oct 26 '06 #1
4 1376
Seems like you should change $number here:
elseif ($question_count < $number) {
$status = "_blank";
}

elseif ($question_count $number) {
$status = "";
}
to some fixed question count you want to use to determine whether the
button should be "inactive" or not.
i.e.

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

$count_inactive_threshold = "2";

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

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

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

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

}
You're comparing a variable that doesn't change in the loop against
your iterator... from your description "based
on the count of questions in a database" it seems you should be
comparing a constant / unchanging variable against another variable
pulled from the database.

HTH--
jason

Oct 26 '06 #2
Rik
st**********@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.

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

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

Ahum, it's not advisable to have a sig-seperator in your post
(--(space)(newline)), as it will automatically trimmed out by correct
newsreaders.

To answer your question: You set $status isn't set when ($question_id !=
$number && $question_count == $number), and $status will be like the last
status from the last loop (hence doubling the selected in this case.

Either start with an uncoditional $status=''; in your loop, or use:
.....
elseif ($question_count <= $number) {
$status = "_blank";
}
.....

--
Rik Wasmus
Oct 26 '06 #3
st**********@hotmail.com wrote:
The script below works great, but with a
strange bug that seems impossible to me.
Take a piece of paper and a pen(cil).
Make four columns for your variables.
Follow the code ...

For the example, assume I renamed the variables

$qc = $question_count;
$qi = $question_id;
$n = $number;

Example:

$qc | $qi | $n | $status |
3 | 2 | 1 | | =$question_id == $number | *NO*
| | | | $question_count < $number | *NO*
| | | | $question_count $number | *YES*
| | | "" | ==q1.gif
| | 2 | "" | =$question_id == $number | *YES*
| | | "_selected" | ==q1_selected.gif
| | 3 | "_selected" | =$question_id == $number | *NO*
| | | | $question_count < $number | *NO*
| | | | $question_count $number | *NO*
| | | | ==q1_selected.gif
| | 4 | "_selected" | =$question_id == $number | *NO*
| | | | $question_count < $number | *YES*
| | | "_blank" | ==q1_blank.gif

--
I (almost) never check the dodgeit address.
If you *really* need to mail me, use the address in the Reply-To
header with a message in *plain* *text* *without* *attachments*.
Oct 26 '06 #4
Rik wrote:
>
Ahum, it's not advisable to have a sig-seperator in your post
(--(space)(newline)), as it will automatically trimmed out by correct
newsreaders.
Proper newsreaders will only pick it up as a sig separator if they are
the only characters on the line. Broken newsreaders, however, can pick
it up anywhere.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Oct 27 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Börni | last post by:
Hi, I have an sql query like this: SELECT column FROM table WHERE column1="3" AND column2="1" This query works perfectly if i run it in the command line, to be exactly it return two results. But...
3
by: Bill C. | last post by:
Hi, I've got a simple console app that just reads an XML file into a DataSet then prints out a description of each table in the DataSet, including column names and row values for each column. ...
5
by: cody | last post by:
I have a very funny/strange effect here. if I let the delegate do "return prop.GetGetMethod().Invoke(info.AudioHeader, null);" then I get wrong results, that is, a wrong method is called and I...
1
by: Sek | last post by:
Hi Folks, I have come across a behaviour which seems strange to me. I wrote the following code for a task: -----BEGIN CODE------- XmlDocument idc = new XmlDocument();...
4
by: =?Utf-8?B?SnVlcmdlbiBELg==?= | last post by:
When I try to run this following small code sample: using System; using System.Collections.Generic; class Program { static void Main(string args) { Dictionary<string, intMyDict = new...
10
by: Michele | last post by:
Please forgive me for the neverending code down here but I cannot find a rational explanation of the output of this simple program (really!). Soluzione class has a double field to represent a...
37
by: Hilton | last post by:
Hi, for (int i = 0; i < list.Count; i++) has a hidden performance hit; i.e. list.Count gets evaluated each time, so we write something like: int listCount = list.Count; for (int i = 0; i <...
3
by: Ken Fine | last post by:
This is a question that someone familiar with ASP.NET and ADO.NET DataSets and DataTables should be able to answer fairly easily. The basic question is how I can efficiently match data from one...
6
by: lukasso | last post by:
Hi, this is my code that should produce something like a timetable for a few days with each day divided into 30 minute pieces. It makes query from MySQL and then creates a 2d $array which then is to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.