我會盡力解決它。爲什麼在我的mysql查詢的WHERE部分中使用常量值並且使用變量時,我沒有在codeigniter中排序
我想在Codeigniter中通過desc或asc對用戶列表進行排序。
我控制器看起來是這樣的 -
/**
* List issues by specified user
*/
public function ByUser($sort_by = 'issue_date', $sort_order = 'desc')
{
/**
* Store _POST value from dropdown in variable - $user_type.
* Pass that value to model to get issues only "by that user"
*/
$user_type = $this->input->post('issues-by-user');
$data['records'] = $this->report_model->issues_by_user($sort_by,$sort_order, $user_type);
$data['main_content'] = 'report';
$data['title'] = 'MarksMate Report';
$this->load->view('includes/template', $data);
}
在我型號,我有一個內部聯接聯接兩個表,並通過特定的用戶獲取的問題。
我型號方法是這樣的 -
public function issues_by_user($sort_by, $sort_order, $user_type)
{
$sort_order = ($sort_order == 'asc') ? 'asc' : 'desc';
$sort_columns = array('id', 'issue_date');
$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'issue_date';
$query = $this->db->select('tblissue.id, student_id, issue_date, issue_title, issue_description, issue_screenshot, first_name, last_name, email_id, role')
->from('tblissue')
->join('tblstudentprofile', 'tblissue.student_id=tblstudentprofile.id')
->where('role', $user_type)
->order_by($sort_by, $sort_order);
$records = $query->get()->result_array();
return $records;
}
查詢工作正常。 (檢查它使用print_r)
但是,當我將URL更改爲 - 「http://localhost/anothertrack/report/ByUser/id/asc」,所有記錄被提取消失。如果我print_r,我看到的只是一個空數組。但是,如果我從->where('role', $user_type)
刪除變量$user_type
,而不是像0或1或2(0,1,2,3是我在數據庫中設置的枚舉值)常數值,然後它的工作原理。
我檢查了mysql查詢的語法,但它很好。我也經歷了Codeigniters URI類手冊。我不知道我的代碼中哪裏可能是錯的。幫我?請?
什麼返回收到的var_dump()GET paramete RS? –
顯然,$ user_type沒有正確設置... – Devon
@SergioIvanuzzo謝謝塞爾吉奧。這是我得到'array(0){}' – pistonracer