我正在使用CodeIgniter框架。我爲我的公司CMS創建搜索功能,搜索有不同的輸入來自表單。有沒有辦法根據$_POST
輸入運行查詢組?我可以運行查詢分開例如:codeigniter查詢分組返回我的表中的所有結果
class Search_model extends CI_Model {
public function get_results($search_term='default')
{
$cat = $_POST["col"];
// Column Select:
$col = 'id, dob, gender, app_date, tests_ordered, payment_method, cost, report_results, country';
// Build the query.
$database = $this->load->database('bookings_dev', TRUE)
->select($col)
->from('results')
->where($cat . ' ' . 'BETWEEN "'. date('Y-m-d', strtotime($_POST['sdate'])). '" and "'. date('Y-m-d', strtotime($_POST['edate'])).'"')
->order_by($_POST["col"]);
// Execute the query.
$query = $database->get();
// Return the results.
return $query->result_array();
}
做工精細的廣告在指定日期範圍內返回正確的記錄(例如所有記錄,我的問題是,當我嘗試和擴大查詢採取其他的選擇考慮到我要麼從數據庫中獲取所有記錄,要麼嘗試更改LIKE和WHERE子句的順序,我嘗試了OR_LIKE,OR_WHERE等方向,任何指向正確的方向都將非常感激,我讀過CIs Query Builder Class Docs,但是內容M正式卡住這裏是我的模型:
<?php
類Search_model擴展CI_Model {
public function get_results($search_term='default')
{
$cat = $_POST["col"];
// Column Select:
$col = 'id, dob, gender, app_date, tests_ordered, payment_method, cost, report_results, country';
// Build the query.
$database = $this->load->database('bookings_dev', TRUE)
->select($col)
->from('results')
->group_start()
->where($cat . ' ' . 'BETWEEN "'. date('Y-m-d', strtotime($_POST['sdate'])). '" and "'. date('Y-m-d', strtotime($_POST['edate'])).'"')
->group_end()
->or_group_start()
->or_like($_POST["col"], $_POST["terms"])
->group_end()
->order_by($_POST["col"]);
// Execute the query.
$query = $database->get();
// Return the results.
return $query->result_array();
}
}
喜歡什麼ID才達到的是: 選擇$的cols FROM結果,其中condtion 1已輸入或者條件2輸入OR條件3等等等等 這可能嗎?
我認爲我們仍然可以在模型中訪問'$ _POST',因爲它是一個超級全局變量。理想情況下,我們應該將變量傳遞給模型方法,但'$ _POST'也可以正常工作。 – cyberrspiritt
我不知道,但CodeIgniter使用MVC模式,我們必須服從它。但是你的表單發送數據給控制器,你不能發送到模型。 –
我肯定從模型訪問$ _POST。它是一個超級全局變量,甚至可以從助手或庫訪問。雖然這種做法不是gud實踐,但我們應該理想地使用$ this-> input來解析get和posts參數。它可以很容易地將安全設置應用於它。 – cyberrspiritt