473,941 Members | 11,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_coun t < $number) {
$status = "_blank";
}

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

Thanks for you attention,
Luc M. Forget

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

elseif ($question_coun t $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_coun t < $count_inactive _threshold) {
$status = "_blank";
}

elseif ($question_coun t >= $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**********@ho tmail.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_coun t <= $number) {
$status = "_blank";
}
.....

--
Rik Wasmus
Oct 26 '06 #3
st**********@ho tmail.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.g if
| | 3 | "_selected" | =$question_id == $number | *NO*
| | | | $question_count < $number | *NO*
| | | | $question_count $number | *NO*
| | | | ==q1_selected.g if
| | 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*******@attgl obal.net
=============== ===
Oct 27 '06 #5

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

Similar topics

9
2467
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 if i run it from php i just get the first of the two results. Any ideas? Mysql 4.1.8 php 5.0.3
3
4622
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. I'm getting some strange results depending the input XML file I use. I was wondering if somebody could help me understand what is going on or point me to a good reference. The code for my program looks like this:
5
1704
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 have no clue why. But if I store the MethodInfo in a local variable I works as expected. I do not understand why this is so, shouldn't both ways be semantically equal?
1
1341
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(); idc.LoadXml("<book><title>The Confidence Man</title><first-name>Herman</first-name><last-name>Melville</last-name></book>");
4
1326
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 Dictionary<string, int>(); MyDict = 0; MyDict = 1;
10
1803
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 matrix. A method named init() fills this matrix with random numbers. The main constructor requires an int param (fCosto) which is _not_used_ inside init(). Every time I instantiate a Soluzione object the matrix is filled by init(). The incredible...
37
2222
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 < listCount; i++)
3
2867
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 dataset to data in a second dataset, using a common key. I will first describe the problem in words and then I will show my code, which has most of the solution done already. I have built an ASP.NET that queries an Index Server and returns a...
6
2257
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 be echoed like a table into html. Almost everything goes well except for one entry going for an hour longer and one disappearing if shorter than 1hour (e.g. 30mins) <?php for($i=0; $i<$numRows; $i++) { $result =...
0
10135
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9968
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11531
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11120
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11297
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10660
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8219
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.