簡體   English   中英

如何在php選擇列表中顯示數據庫中的optgroup標簽

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

**表**-ServiceAreaCategory

欄位 -ID,名稱

-技能

字段 -id,技能名稱,service_area_category_id

在選擇ServiceAreaCategory ID時,我從技能表中獲取了Skill_name。

現在,我想在狀態列表框的optgroup中顯示ServiceAreaCategory表的名稱。

我有一個依賴的下拉列表框,如國家和州...在選擇國家時,我正在通過Ajax獲取選定國家的州。

現在,我想在州的optgroup國家列表框中顯示國家標簽。

對於前-當我從國家/地區列表框中選擇印度和美國時。

狀態列表框顯示結果如下。

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

任何人都可以幫助我在選擇框中獲取並顯示所選狀態的optgroup標簽。

下面是我的代碼。

//用於顯示國家

<?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. 首先從表ServiceAreaCategory中找到不同的記錄,並將名稱存儲在數組中

     $ServiceAreaCategory = array( 'serviceName1', 'serviceName2', 'serviceName3', 'serviceName4', 'serviceName...N', ); 
  2. 現在准備一個黑白表的連接記錄ServiceAreaCategory and Skill

  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']); } } 

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