如果数据存储为内爆,我们如何使用where条件

时间:2018-05-18 05:29:22

标签: php codeigniter-3

这里我的表看起来像这样

enter image description here

如果location_id3,我希望得到所有部门,这意味着如果使用了地址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

请帮我解决我的问题

2 个答案:

答案 0 :(得分:1)

希望这会对您有所帮助:

您必须首先查询以获取department并使用explode

将location_id转换为数组

您的模型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;
}
  

为此目的,有一个名为FIND_IN_SET的函数。您可以在https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_find-in-set

下的mysql文档中找到它