doumicheng6732 2012-09-18 13:30
浏览 19
已采纳

如何防止组合框中的重复值

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?

  • 写回答

3条回答 默认 最新

  • dongsu7049 2012-09-18 13:35
    关注

    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>'."
    ");
                echo '<option value="' . $key . '">' . $country['name'].'</option>'."
    ";
    
                // Generate $states box
                foreach ($country['states'] as $key => $state) {
                    echo ($key.'<BR>'."
    ");
                    echo '<option value="' . $key . '">' . $state['name'].'</option>'."
    ";
    
                    // Generate $city box
                    foreach ($state['cities'] as $key => $city) {
                        echo ($key.'<BR>'."
    ");
                        echo '<option value="' . $key . '">' . $city.'</option>'."
    ";
                    }
    
                }
    
            }
    
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么