0

显示数据库搜索结果,但单击页面链接 2 时分页链接不起作用?这里是我的控制器函数 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>

请指导我自己尝试这么多但没有得到正确的结果?

4

2 回答 2

0

使用 codeigniter 应用程序搜索的最佳方法是我发现非常有用的以下关于 nettuts 的教程,您可以使用多个字段来搜索记录和分页真的很容易看看它

搜索结果

于 2013-02-18T10:16:52.787 回答
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;

}
于 2013-02-18T15:16:31.670 回答