Jump to content

Nested Loop for "Cities in Regions" Menu by State


Dizzle

Recommended Posts

I've searched but can't seem to find anything I can apply to this situation.  Most of the resources are dedicated to dropdown menu's from the same table. 

 

I have two tables 'regions' and 'cities' and I am looking to create a menu that lists all the cities within a region then moves on to do the same for the rest of the regions in that state:

 

State1

Region1

City1

City2

City3

Region2

City1

City2

City3

Region3

Etc

 

My tables:

States table: stateID, state

Regions table: regionID, region, stateID

Cities table: cityID, city, regionID

 

I currently have two pages- select region, then select city... I'd like to consolidate to one step after choosing a state.

 

I know I need a nested loop, (while?), and to count the results somehow to retrigger loop; also not sure how to handle two mySQL querries in the nested loop.  I'm having trouble wrapping my mind around it. Thank you.

Link to comment
Share on other sites

If I'm understanding correctly, you'd probably want to do your sql query first and put everything into a multidimensional array, then use a foreach loop to get what you would like out of it in the formatting you need. I hate to give you links to tutorials, but someone else likely worded it better than I can. Here's php.net multidimensional array: http://php.net/manual/en/function.array.php and also foreach loop: http://us2.php.net/manual/en/control-structures.foreach.php  Hope that helps some.

Link to comment
Share on other sites

-user clicks 'state' from a static menu on the first page.

-"stateID" is then passed through GET where the all the "regions" with that "stateID" are called for...

 

*I'm trying to call for all the "regions" in that "state" AND the "cities" in each "region"  all in one step; from different tables. 

Link to comment
Share on other sites

Not looking to make into a form (thanks for the link tho).  Something more like this:

 

***Not dropdown menu, just a menu with the cities as links to city results...

 

WYOMING <--- State selected from previous page...

Eastern <--- Region1

Big Horn <---City1 in Region1

Buffalo   <--- City2 in Region2

Casper

 

Western <--- Region2

Dubois

Lander

 

Jackson Hole Wyoming <--- Region3

Jackson

Jackson Hole

Pinedale

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.