如何在php选择列表

时间:2016-08-04 07:10:00

标签: php cakephp

**表** - ServiceAreaCategory

字段 - id,name

- 技能

字段 - id,skill_name,service_area_category_id

选择ServiceAreaCategory id我从技能表中获取skill_name。

现在,我想在状态列表框的optgroup中显示ServiceAreaCategory表的名称。

我有一个依赖的下拉列表框,如国家和州......在选择国家时,我通过ajax获取所选国家/地区的状态。

现在我想在州列表框的optgroup中显示标签。

为前 - 当我从国家列表框中选择印度和美国时..

状态列表框显示如下的结果..

india
--abc
--def
America
--abc
--def

任何人都可以帮助我在选择框中获取并显示我所选州的选项组标签。

下面是我的代码..

//用于显示国家/地区

<?php
                                echo $this->Form->input('UserLogDetail.service_area_category_id', array(
                                            'id' => 'shipping_type',
                                            'required' => false, 
                                            'multiple' =>'multiple',
                                            'type' => 'select',                                           
                                            'class' => 'form-control',
                                            'label' => false,
                                            'options' => $serviceCategory
                                ));
                                ?>

                            **// for displaying states of selected countries**


 <?php
                            echo $this->Form->input('UserLogDetail.skills', array(
                                'class' => 'selectpicker',
                                'required' => false,
                                'multiple' =>'multiple',
                                'id' => 'skills',
                                'label' => false,
                                'options' => '$skills'                                           
                            ));
                            ?>  

**//contoller functions**



 public function getServiceArea(){     
        $this->loadModel('ServiceAreaCategory');        
        $serviceCategory = $this->ServiceAreaCategory->find('list',array('conditions'=>array('is_active'=>1),'fields'=>array('ServiceAreaCategory.id','ServiceAreaCategory.name'),'order'=>'name ASC'));
        $this->set('serviceCategory',$serviceCategory);     
    }

    public function loadSkills() {      
        $this->loadModel('Skill');
        $skills = array();
        if (isset($this->request['data']['id'])) {
        $ids = explode(",",$this->request['data']['id']);
        if(count($ids)>1){
            $skills = $this->Skill->find('list', array('fields' => array('Skill.id','Skill.skill_name'),'conditions' => array(
            'Skill.service_area_category_id IN' => $ids)));
            } else {
            $skills = $this->Skill->find('list', array('fields' => array('Skill.id','Skill.skill_name'),'conditions' => array(
        'Skill.service_area_category_id' => $ids)));
            }          
        }
        echo json_encode($skills);
        exit();
    } 



**Ajax function**


<script type="text/javascript">
    $(document).ready(function() {        
        $("#shipping_type").on('change', function() {
            var id = $(this).val();           
            if (id) {
                var dataString = 'id=' + id;
                $.ajax({
                    type: "POST",
                    url: '<?php echo Router::url(array("controller" => "Profiles", "action" => "loadSkills"),true); ?>',
                    data: dataString,
                    dataType: 'JSON',
                    cache: false,                  
                   success: function(html) {                       
                        $("#skills").html("");
                        $.each(html, function(key, value) {
                            $('<option>').val('').text('select');
                            $('<option>').val(key).text(value).appendTo($("#skills"));
                        });
                        $('#skills').selectpicker('refresh');
                    }
                });
            }
        });      
});
</script>

1 个答案:

答案 0 :(得分:0)

  1. 首先从表 ServiceAreaCategory 中查找不同的记录 并将名称存储在数组

    $ServiceAreaCategory = array(
        'serviceName1',
        'serviceName2',
        'serviceName3',
        'serviceName4',
        'serviceName...N',
    );
    
  2. 现在准备连接记录黑白表 ServiceAreaCategory 技能

  3. 现在您已从第2步获取数据集对象并存储在变量$data
  4. 循环遍历$ data并添加适用于数组$ServiceAreaCategory

    中服务名称的选项
    foreach($data as $row){
         if(in_array($row['name'], $ServiceAreaCategory)){
              $ServiceAreaCategory[$row['name']][] = array($row['id'] => $row['skill_name']);
         }
    }
    
  5. HTML:

    $html = "<select>";
    foreach($ServiceAreaCategory as $key => $value){
        if(!empty($value)){
            $html .="<optgroup label='".$key."'>"; 
            foreach($value as $k => $v){
                $html .="<option value='".$k."'>".$v."</option>";
            }
            $html .="</optgroup>"; 
        } 
    }
    
    $html .= "</select>";
    
    echo $html;