2017-08-17 77 views
0

我在MySQL与数据表和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

谢谢

回答

1

应该是这样的:

public function get_myreports($user_id, $searching) 
{ 
    $this->db->->group_start()  
    ->or_like($searching) 
    ->group_end() 
    ->where('user_id', $user_id); 
    return $this->db->get('reports')->result_array(); 
} 
+0

我几乎在那里,但它创造了这样的东西,它只是添加一个AND和.......选择*从'报告'WHERE'report_id' LIKE'%sup%'ESCAPE'!' 'OR'report_received' LIKE'%sup%'ESCAPE'!' AND()和'user_id' = '4' ORDER BY'report_status' DESC,'report_id' LIMIT 10 – rjcode

+0

'$搜索= $这个 - > DB-> or_like($列[$ i]于,$ searchValue);'应就像一个数组,然后or_like将做派生的分组任务: '$ searching [$ columns [$ i]] = $ searchValue;' – Riad

+1

现在又有一个问题,当我删除所有关键字进行搜索时,现在它显示数据表中的错误,因为搜索无法完成,..... SELECT * FROM'ec_reports' WHERE(LIKE'%%'ESCAPE'!')AND'user_id' ='4'ORDER BY'report_status' DESC,' report_id'限制10 – rjcode

0

尝试this.Add的全部或情况

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 
0

括号可能是这样的查询。在搜索中添加()括号。

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 
+0

好吧,那么现在我需要知道怎样才能建立在CI框架查询括号 – rjcode

+0

$这个 - >模型 - > get_myreports($这个 - > session-> userdata('user_id'),'('。$ searching。')');使用连接。 –

0
$this->model->get_myreports($this->session->userdata('user_i‌​d'), '('.$searching.')'); 

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