显示数据库搜索结果,但点击页面链接2时分页链接无效? 这里我的Controller函数pagesearch从我的视图文件中获取search_term,它是输入文本字段以供搜索的单词
<?php
class Pagesearch extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('searchmodel');
$this->load->helper('url');
// Load Pagination
$this->load->library('pagination');
}
public function execute_search($offset=0)
{
$search_term = $this->input->post('tsd');
echo $search_term;
$pagination_per_page = 3;
// Config setup
$config['base_url'] = site_url('pagesearch/execute_search');
$config['per_page'] = $pagination_per_page;
$config['total_rows'] = count($this->searchmodel->get_results($search_term));
$config['uri_segment'] = 2;
$config['use_page_numbers'] = TRUE;
$this->pagination->initialize($config);
$data['deals_data'] = $this->searchmodel->get_results($search_term, $pagination_per_page, (($offset != 0)?( $pagination_per_page * ($offset - 1)): 0));
$this->load->view('deals/jsonsearch',$data);
}
}
here it is my model for like query where $search
class Searchmodel extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_results($search_term, $limit, $offset=0)
{
$this->db->select('*');
$this->db->like('name',$search_term);
$query = $this->db->get('deals',$limit,$offset);
return $query->result_array();
}
}
here it is view code look like this
<div id="dealsData">
<?php foreach ($deals_data as $data): ?>
//some code
<?php endforeach ?>
<div id="pagination">
<?php echo $this->pagination->create_links(); ?>
</div>
</div>
请指导我自己尝试这么多,但没有得到正确的结果?
答案 0 :(得分:0)
使用codeigniter应用程序进行搜索的最佳效果我觉得非常有用的是关于nettuts的以下教程,您可以使用多个字段来搜索记录,并且分页很容易看到它
答案 1 :(得分:0)
这是一个例子,我是如何实现的
在控制器中
function index()
{
$this->load->library('pagination');
$config = array();
$config["base_url"] = base_url() . "city/index";
$config["total_rows"] = $this->city_model->record_count();
$config["per_page"] = 20;
$config["uri_segment"] = 4;
$this->pagination->initialize($config);
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
$data["city"] = $this->city_model->get_cities($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
}
在模型中
function record_count()
{
return $this->db->count_all("city");
}
function get_cities($limit,$start)
{
$this->db->limit($limit,$start);
$this->db->select('*');
$this->db->from('city');
$this->db->order_by('city_name');
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}