这里我的表看起来像这样
如果location_id
为3
,我希望得到所有部门,这意味着如果使用了地址ID 3
,我需要获取cardiology
和{{1}的department_names这样我就像这样完成了我的代码
我的hair
看起来像这样
controller
我的public function get_location_departments()
{
$d = $_POST['location'];
$data['departments'] = $this->Hospital_model->get_location_departments($d);
$this->load->view('frontend/ajax_get_departments',$data);
}
看起来像这样
model
请帮我解决我的问题
答案 0 :(得分:1)
希望这会对您有所帮助:
您必须首先查询以获取department
并使用explode
您的模型get_location_departments
方法应如下所示:
public function get_location_departments($d)
{
if (! empty($d))
{
$query = $this->db->get('department');
if ($query->num_rows() > 0 )
{
foreach ($query->result() as $location)
{
$location_ids = explode(',',$location->location_id);
if (in_array($d, $location_ids))
{
$data[] = $location;
}
}
}
return $data;
//print_r($data);
}
}
答案 1 :(得分:1)
imho正确的答案是
public function get_location_departments($d)
{
$query = $this->db
->where('FIND_IN_SET('.$this->db->escape($d).', location_id)', NULL, false)
->get('department')
->result();
return $query;
}
为此目的,有一个名为
下的mysql文档中找到它FIND_IN_SET
的函数。您可以在https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_find-in-set