Jump to content

For loop problem


stmosaic

Recommended Posts

I'm integrating a dynamic columns script into my state/city script, but am having problems getting the cities to show. It's only displaying 1 city, the first alphabetically, when it should be showing 3. So that seems to indicate to me that the problem is the for loop not interating through the array. But this for loop works on my other pages where it is not within the while loops of the state/city script. So, either there is some conflict going on or something else I'm not seeing. An other pair of eyes looking at this would be wonderful. Thank you so much for your help!

 

Output:

State1 (should have a city showing)

State2 (should have 2 cities showing)

  City1

 

//State  query 
$result = mysql_query("SELECT DISTINCT State FROM $DBTABLE WHERE Active='Y' ORDER BY State ASC");
  while($row = mysql_fetch_array($result))
{
//Echo out each State
 echo "<tr><td valign=\"top\" bgcolor=\"#FDD9BA\"> $row[state]</td></tr><tr><td>";
//City query	 
$sub = mysql_query("SELECT City, CLSfolder FROM $DBTABLE WHERE Active='Y' AND State='{$row['State']}'");     
     while($subrow = mysql_fetch_array($sub))
     {
//Section to echo out each City.
  //set the number of columns and determine the number of rows
   $columns = 3;
   $num_rows = mysql_num_rows($sub);
  if ($num_rows != 0 )
  {
    echo "<TABLE width=\"100%\" border=1 align=\"center\">\n";

    //this for loop is so we can use the number of rows
    for($i = 0; $i < $num_rows; $i++) {
    $subrow = mysql_fetch_array($sub);
    if($i % $columns == 0) {
        //if there is no remainder, we want to start a new row
        echo "<TR>\n";
    }
    echo
"<td width=\"25%\" align=\"left\" valign=\"top\"><a href=\"http://www.domainname.com/$subrow[CLSfolder]\" target=\"_blank\"><font face=\"Verdana\" size=2><li>$subrow[City]</li></a><br></td>";
    if(($i % $columns) == ($columns - 1) || ($i + 1) == $num_rows) {
        //if there is a remainder of 1, end the row
        //or if there is nothing left in our result set, end the row
        echo "</TR>\n";
    }
    }
echo "<tr><td> </td></tr></TABLE>";
   }
     }
}
echo "</td></tr></table>" ; 

mysql_close();

Link to comment
https://forums.phpfreaks.com/topic/171297-for-loop-problem/
Share on other sites

I've been working on this the past couple of hours and put in more data to get a clearer picture of the problem. What I've discovered is that I'm definately missing something here, but don't know what. The issue seems to be within the for loop, but the output is weird.

 

It outputs all the right number of <li> dots, but one city within each state doesn't show up. I am unable to determine a rhyme or reason for the one city that doesn't show up. It's not alphabetical, it's not ID number... so I'm at a loss to explain this behavior. Would greatly appreciate any help.

Link to comment
https://forums.phpfreaks.com/topic/171297-for-loop-problem/#findComment-903446
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.