Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop Problem 3

Status
Not open for further replies.

TheConeHead

Programmer
Joined
Aug 14, 2002
Messages
2,106
Location
US
What is wrong with this? I get to records of 'VA'... It does not move onto 'WV'

<?
$state_array = array(&quot;VA&quot;,&quot;WV&quot;);
$stArrPlace = 0;

while ($strArrPlace < 2) {
$result = mysql_query(&quot;SELECT * FROM counties where state = '$state_array[$stArrPlace]'&quot;);
$num_rows = mysql_num_rows($result)+1;
$count=1;
print &quot;case '$state_array[$stArrPlace]':\n&quot;;
print &quot;document.forma.countyI.options.length = $num_rows;\n&quot;;
print &quot;document.forma.countyI.options[0] = new Option('Mine Is Not Listed', 'None');\n&quot;;
while ($a_row = mysql_fetch_array($result)) {
print &quot;document.forma.countyI.options[$count] = new Option('$a_row[name]', '$a_row[id]');\n&quot;;
$count++;
}
$strArrPlace++;
}
print &quot;break;\n\n&quot;;
?>

[conehead]
 
I can't see anything wrong with it...
You have an array with a size of 2 (indexs 0 and 1),
taking away the work, you have
Code:
while($index < 2)
{
  //do stuff with $array[$index]
  $index++
}

Try changing the while (the big one) to a for or a foreach. Also, you could do this with one DB query to lessen the chances of timeout, and then you'd also be removing a loop (and thus be lessening the chances of errors).
 
I see what's wrong with it.

In the query line, you reference:

[tt]$state_array[$stArrPlace][/tt]

but later in the script, you increment:

[tt]$strArrPlace++;[/b]

Since the appropriate variable does not get incremented, your script runs the query twice with the same state.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
why don't you use the in option for the query

&quot;SELECT * FROM counties where state in('VW','WV')&quot;);

and loop through the query result.

and it's also possible to convert an array so you can easily use it as a variable in your sql statement. but I don't know that one out of my head. thought it was done with explode or implode
 
sleipnir214 - that's it dude! a simple typo... thanks for all of the other suggesstions!

[conehead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top