[英]CodeIgniter Search by string using get
I am stuck with writing View for my code igniter search form which need to use get.. 我坚持为我的代码点火器搜索表单编写View,这需要使用get ..
I currently have this 我目前有这个
Controller 控制者
<?php
class Search extends CI_Controller {
function __construct(){
parent::__construct();
}
public function index(){
$this->load->view('Search');
}
public function doSearch()
{
$this->load->model("Messages_model");
if ($this->input->get('search') !== FALSE) {
$data ['results'] = $this->Messages_model->searchMessages($this->input->get('search'));
} else {
$data['results'] = array();
}
$this->load->view("Search", $data);
}
Model 模型
class Messages_model extends CI_Model{
function searchMessages($string){
$this->load->database();
$query = $this->db->query("SELECT * FROM messages WHERE text LIKE '%$string%'");
return $query->result();
}
View 视图
<!DOCTYPE html>
<html>
<style>
</style>
<head>
<title></title>
</head>
<body>
<form action="<?php echo site_url('Search/doSearch');?>" method = "get">
<input type="text" name = "keyword"/>
<input type="submit" value = "Search" />
</form>
</div>
</body>
</html>
Can anyone help me out in getting search string displayed 任何人都可以帮助我显示搜索字符串
Firstly, go to application/config/config.php and find $config['allow_get_array']
and make sure it's set to TRUE
. 首先,转到application / config / config.php并找到
$config['allow_get_array']
并确保将其设置为TRUE
。
Then in your controller you would have something like this: 然后在您的控制器中,您将得到以下内容:
public function search()
{
$this->load->model("Messages_model");
if ($this->input->get('search') !== FALSE) {
$data ['results'] = $this->Messages_model->searchMessages($this->input->get('search'));
} else {
$data['results'] = array();
}
$this->load->view("Search", $data);
}
Please note that there isn't any validation happening on the string here so you will need to add your own. 请注意,这里的字符串没有任何验证,因此您需要添加自己的验证。
Hope this helps! 希望这可以帮助!
EDIT 编辑
View file: 查看文件:
<!DOCTYPE html>
<html>
<style>
</style>
<head>
<title></title>
</head>
<body>
<form action="<?php echo site_url('search/doSearch'); ?>" method = "get">
<input type="text" name="keyword" value="<?php echo isset($search_value) ? $search_value : ''?>"/>
<input type="submit" value="Search" />
</form>
<?php
if ($search_passed && !empty($results)) {
foreach ($results as $result) {
//Code for displaying results
}
} elseif ($search_passed && empty($results)) {
echo 'No results found!';
}
?>
</div>
</body>
</html>
Controller Method: 控制器方式:
public function doSearch() { $this->load->model("Messages_model"); 公共功能doSearch(){$ this-> load-> model(“ Messages_model”);
if ($this->input->get('keyword') !== FALSE) {
$data ['results'] = $this->Messages_model->searchMessages($this->input->get('keyword'));
//Uncomment the line below to test
// echo '<pre>'; print_r($data['results']);die('</pre>');
$data['search_passed'] = TRUE;
$data['search_value'] = $this->input->get('keyword');
} else {
$data['search_passed'] = FALSE;
$data['results'] = array();
}
$this->load->view("Search", $data);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.