【发布时间】:2017-08-17 05:54:11
【问题描述】:
我正在使用 DataTables 和 CodeIgntier 在 mySql 中搜索数据,
下面是控制器搜索循环和模型,
控制器搜索构建
if (isset($searchValue) && $searchValue != '')
{
$searching = array();
for ($i=0; $i<count($columns); $i++) //Loop search in all defined columns
{
$searching = $this->db->or_like($columns[$i], $searchValue);
}
}
else
{
$searching = NULL;
}
$this->model->get_myreports($this->session->userdata('user_id'), $searching);
模型函数,
public function get_myreports($user_id, $searching)
{
$this->db->where('user_id', $user_id);
$searching;
return $this->db->get('reports')->result_array();
}
这又会导致下面的 sql 查询,
SELECT * FROM `reports` WHERE `report_id` LIKE '%sup%' ESCAPE '!' OR `report_name` LIKE '%sup%' ESCAPE '!' OR `report_submitted` LIKE '%sup%' ESCAPE '!' OR `report_total` LIKE '%sup%' ESCAPE '!' OR `report_status` LIKE '%sup%' ESCAPE '!' OR `report_received` LIKE '%sup%' ESCAPE '!' AND `user_id` = '4' ORDER BY `report_status` DESC, `report_id` LIMIT 10
现在的问题是,
它在进行搜索时显示所有用户报告,即使我告诉使用 user_id = 4 进行搜索,它也显示 user_id = 1 的报告,而仅在进行搜索时,
在正常的datatables 页面加载时,它会显示正确的 user_id = 4 报告,但问题仅在搜索查询中,
如何只搜索user_id = 4?
谢谢,
【问题讨论】:
标签: php mysql codeigniter datatables