I have a view in my codeigniter app that gets an array from the database. I want to create several combo boxes based on the contents of the array.
Here's what the array looks like:
Array (
[0] => Array (
[L1ID] => 2
[L1Location] => USA
[L2ID] => 3
[L2Location] => New York
[L3ID] => 4
[L3Location] => Manhanttan
)
[1] => Array (
[L1ID] => 2
[L1Location] => USA
[L2ID] => 8
[L2Location] => New Jersey
[L3ID] => 7
[L3Location] => Bergen County
)
[2] => Array (
[L1ID] => 5
[L1Location] => Canada
[L2ID] => 12
[L2Location] => Ontario
[L3ID] => 50
[L3Location] => Toronto
)
[3] => Array (
[L1ID] => 6
[L1Location] => South Korea
[L2ID] => 22
[L2Location] => Gyungido
[L3ID] => 25
[L3Location] => Buchon
)
)
As you can see, each item in the array can have up to 3 locations defined within. I would like to create a combo box that has all the Location 1's in it. So, in other words, it will have "USA", "CANADA", "SOUTH KOREA" as options. When the user clicks on any location, i want to query the same array and get the next level down - all the location 2's - populated in a separate combo box. So for example, when they select "USA" in location 1, location 2 combo will show "New York" and "New Jersey" as options.
I've written some code to start looping through the array and extract all L1's (location 1's) but I don't know how to prevent duplicates from being added.
Here's my code:
<select id="L1locationlist" name="L1locationlist">
<option value=""></option>
<?php
foreach ($locations as $location) {
echo ($location['L1ID'].'<BR>');
echo '<option value="' . $location['L1ID'] . '">' . $location['L1Location'].'</option>';
}
?>
</select>
I only want one USA entry in the combo box. Can you tell me how I can do this? Do I have to create a separate function to check if it already exists in the combo box? Thank you.
EDIT:
The original plan was to get all location information once from the database, and then on the client side, somehow dynamically drill down into the different locations depending on what the user clicks. But I guess I should also ask whether this is a good design or not? Perhaps the code will be "cleaner" if I just make separate calls to the database. So for example, I will initially just get all location 1 values. Then if the user selects "USA" i will query the db again for all sublocations in USA. I guess the initial thought was to save multiple trips to the db. Any comments?