简体   繁体   English

如何在php选择列表中显示数据库中的optgroup标签

[英]how to display optgroup label from database in php select list

**Table ** - ServiceAreaCategory **表**-ServiceAreaCategory

fields - id,name 栏位 -ID,名称

Table - Skill -技能

fields - id,skill_name,service_area_category_id 字段 -id,技能名称,service_area_category_id

on selection of ServiceAreaCategory id i am getting skill_name from skill table. 在选择ServiceAreaCategory ID时,我从技能表中获取了Skill_name。

Now,i want to display name of ServiceAreaCategory table in optgroup of state listbox. 现在,我想在状态列表框的optgroup中显示ServiceAreaCategory表的名称。

I have a dependent dropdown list box like country and state...on selection of countries i am fetching states of selected countries via ajax. 我有一个依赖的下拉列表框,如国家和州...在选择国家时,我正在通过Ajax获取选定国家的州。

now i want to show countries lable in optgroup of states listbox. 现在,我想在州的optgroup国家列表框中显示国家标签。

for ex - when i select india and america from country listbox.. 对于前-当我从国家/地区列表框中选择印度和美国时。

The state listbox shows result like below.. 状态列表框显示结果如下。

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

can anybody help me to fetch and display optgroup label of my selected state in select box. 任何人都可以帮助我在选择框中获取并显示所选状态的optgroup标签。

below is my code.. 下面是我的代码。

// for displaying countries //用于显示国家

<?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. First find the distinct record from table ServiceAreaCategory and store the names in the array 首先从表ServiceAreaCategory中找到不同的记录,并将名称存储在数组中

     $ServiceAreaCategory = array( 'serviceName1', 'serviceName2', 'serviceName3', 'serviceName4', 'serviceName...N', ); 
  2. Now Prepare a join record b/W table ServiceAreaCategory and Skill 现在准备一个黑白表的连接记录ServiceAreaCategory and Skill

  3. Now you have get an data-set object from the step 2 and stored in variable $data 现在,您从步骤2中获得了一个数据集对象,并将其存储在变量$data
  4. Loop thorough $data and add the option appropriate to Service name in array $ServiceAreaCategory 循环遍历$ data并在$ServiceAreaCategory数组中添加适合于服务名称的选项

     foreach($data as $row){ if(in_array($row['name'], $ServiceAreaCategory)){ $ServiceAreaCategory[$row['name']][] = array($row['id'] => $row['skill_name']); } } 

HTML : 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM