我想使用php从多个选择中填充我的下拉菜单,但是当我的查询的某些值返回null时,下拉菜单无法获取整体值,但我遇到了问题。这是我的数组json编码的示例。
[{"A"},{"B"}][][][][{"D"},{"E"},{"F"},{"G"},{"H"}][{"I"},{"J"}]
这是我在控制器上的查询代码
for ($i=0; $i<count($customerField); $i++) {
for ($j=0; $j<count($projectField); $j++) {
for ($k=0; $k<count($user_roleField); $k++) {
$customer = $customerField[$i];
$project = $projectField[$j];
$user_role = $user_roleField[$k];
$query = $this->db->query("SELECT user_id
FROM `ixt_user_project_list`
LEFT JOIN ixt_user_type ON ixt_user_project_list.user_type = ixt_user_type.user_type
WHERE ixt_user_project_list.user_cust_id ='".$customer."'
AND ixt_user_project_list.user_project_id ='".$project."'
AND ixt_user_type.user_owner ='".$user_role."'")->result_array();
echo json_encode($query);
}
}
}
这是我的js视图
$('select#roleField').on('change', function () {
var customer = $('select#customerField').val();
var project = $('select#projectField').val();
var role = $('select#roleField').val();
var data = {
customer: customer,
project: project,
role: role,
"<?=$this->security->get_csrf_token_name()?>" : "<?=$this->security->get_csrf_hash()?>"
};
$.post('<?php echo base_url(); ?>Dashboard/get_userid', data, function(json) {
userid_data = '<option value="All" selected>All</option>';
$.each(json, function(index, obj){
userid_data += '<option value="'+obj.user_id+'">'+obj.user_id+'</option>';
});
$userid.html(userid_data);
}, 'JSON');
});
有人可以帮助我解决我的问题吗?谢谢。