In a perfect world Dynamic Dependent Dropdown Menus are really easy, and generally for me quite reliant on the database structure. When I create a site and i know before starting that it might use geographic entities I might have a database structure like this.
Continent Table
id | continent |
---|---|
1 | Africa |
2 | Europe |
3 | Asia |
Country Table
id | country | continent foreign key id |
---|---|---|
1 | Algeria | 1 |
2 | Angola | 1 |
3 | Benin | 1 |
4 | Botswana | 1 |
5 | Armenia | 2 |
6 | Albania | 2 |
7 | Andorra | 2 |
8 | Austria | 2 |
9 | Afghanistan | 3 |
10 | Azerbaijan | 3 |
11 | Bahrain | 3 |
12 | Bangladesh | 3 |
City Table
id | city | continent foreign key id | country foreign key id |
---|---|---|---|
1 | Algiers | 1 | 1 |
2 | Oran | 1 | 1 |
3 | Ambriz | 1 | 2 |
4 | Andulo | 1 | 2 |
5 | Aragatsotn Province | 2 | 5 |
6 | Ararat Province | 2 | 5 |
7 | Armavir Province | 2 | 5 |
8 | Tirana | 2 | 6 |
9 | Kabul | 3 | 9 |
10 | Kandahar | 3 | 9 |
11 | Agdash | 3 | 10 |
12 | Manama | 3 | 11 |
So thats a perfect world example.
And here is a real world example.
I start off a simple website that is for a single hotel in Washington DC, I complete the website and all are happy. On the website there is an enquiry form that takes user details.
First Name
Last Name
Email
Physical Address
Enquiry
â Sign up to our Newsletter
The users complete the form and 2 things happen, an email gets sent to the hotel, and an entry gets stored in the database, this entry has some added info like a datetime lets say.
So far all is looking quite straight forward, but 3 years later the client asks for a backend system so they can see all these enquiries as well as many other things. They want to create a mailing list to communicate with all these users.
Now my SQL table looks something like this
id | fname | lname | add1 | add2 | addCountry | source | status | date | |
---|---|---|---|---|---|---|---|---|---|
1 | First | Last | a@g.com | Road | Town | Africa | Website | Subscribed | 2-10-2015 10:32:15 |
2 | First | Last | b@g.com | Road | Town | USA | Website | Unsubscribed | 7-10-2015 09:02:54 |
You get the idea, for the mailing list they want to filter all this data by Status then Source then Country
So they want 3 select menus to be dynamically filled with all this data
serverconnect1 - I make a simple query to get all records for display in a table
I then bind serverconnect1 to the status select menu with value and title set to status
I then bind serverconnect1 to the source select menu with value and title set to source
I then bind serverconnect1 to the countries select menu with value and title set to countries
My selects are now dynamic and dependant on what is selected but there are 100 duplicate entries for each record, and after trying multiple things, such as data formatters of unique I can not get it sorted out.
So I add other queries to only get distinct values and multiple conditions to only show dependant on the first selection, but each time a select is used it updates the others and they revert to the first value.
serverconnect2 - I query the same table but only the status field to get distinct values
serverconnect3 - I query the same table but only the source field to get distinct values
serverconnect4 - I query the same table but only the countries field to get distinct values
I must be just overthinking this, anyone got some ideas for me how to best do this, would be great to keep just the one query solution but get rid of all the duplication.
Sorry for the long post and explanation.