[英]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>
首先从表ServiceAreaCategory中找到不同的记录,并将名称存储在数组中
$ServiceAreaCategory = array( 'serviceName1', 'serviceName2', 'serviceName3', 'serviceName4', 'serviceName...N', );
现在准备一个黑白表的连接记录ServiceAreaCategory and Skill
$data
循环遍历$ 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.