Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8545179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:44:04+00:00 2026-06-11T12:44:04+00:00

I have a view in my codeigniter app that gets an array from the

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:44:06+00:00Added an answer on June 11, 2026 at 12:44 pm

    Loop through the $locations array and generate a new array for each box, checking values as you go. Then, loop through each of those arrays and generate the boxes. (Demo.)

        <?php
            $countries = array();
            $states = array();
            $cities = array();
            foreach ($locations as $location) {
                if(!array_key_exists($location['L1ID'], $countries))
                    $countries[$location['L1ID']] = array(
                        'name' => $location['L1Location'],
                        'states' => array(),
                    );
                if(!array_key_exists($location['L2ID'], $countries[$location['L1ID']]['states'])){
                    $countries[$location['L1ID']]['states'][$location['L2ID']] = array(
                        'name' => $location['L2Location'],
                        'cities' => array(),
                    );
                }
                if(!array_key_exists($location['L3ID'], $countries[$location['L1ID']]['states'][$location['L2ID']])){
                    $countries[$location['L1ID']]['states'][$location['L2ID']]['cities'][$location['L3ID']] = $location['L3Location'];
                }
            }
    
            // Generate $countries box
            foreach ($countries as $key => $country) {
                echo ($key.'<BR>'."\n");
                echo '<option value="' . $key . '">' . $country['name'].'</option>'."\n";
    
                // Generate $states box
                foreach ($country['states'] as $key => $state) {
                    echo ($key.'<BR>'."\n");
                    echo '<option value="' . $key . '">' . $state['name'].'</option>'."\n";
    
                    // Generate $city box
                    foreach ($state['cities'] as $key => $city) {
                        echo ($key.'<BR>'."\n");
                        echo '<option value="' . $key . '">' . $city.'</option>'."\n";
                    }
    
                }
    
            }
    
        ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form that submits data to a database in a CodeIgniter app
I'm using codeigniter and have a main view that calls another view based on
Currently I have this url to view an image from the db (codeigniter) domain.com/view/id
I have a view that generates a dynamic navigation bar. Within Codeigniter, I just
I have a small app in codeigniter. There is a form that has 3
I have a CodeIgniter app that works on one server (IIS 6) just fine.
i have written a small codeigniter test app that is currently running on my
Using codeigniter 2.1.2, I have a form view loaded through a controller that takes
i am using codeigniter. i have a table to be displayed in a view
I have view stack in a app like so: <mx:ViewStack id=viewStack left=0 right=0 top=0

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.