使用带搜索的查询字符串进行CodeIgniter分页

时间:2017-03-26 08:26:51

标签: php codeigniter pagination

我是CodeIgniter的新手。我被困在一些寻找分页的地方。有人帮助我。

class Pagination2 extends My_controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->database();
        $this->load->library('pagination');
        $this->load->model('pagination_model');
        $userdata=$this->session->userdata('userdata');
        if(!isset($userdata['user_role']))
        {
            redirect(base_url('admin'));
        }
    }

    public function manage($page = 0)
    {

       $this->load->library('pagination');
       $per_page = 10;   
       $_POST = $_GET;
       $search= $this->input->get('book_name')?$this->input->get('book_name'):'NIL';
       $this->db->limit($per_page,$page);
       if($search=='NIL')
       {
        $data['content_fb'] = $this->db->get('tbl_books')->result();
       }
       else
       {
        $this->db->like('name', $search);
        $res = $this->db->get('tbl_books');
        $data['content_fb'] =  $res->result();
       }
       // $data['content_fb'] = $this->db->select('id')->limit($per_page,$page)->get('content_fb')->result();

        $config['base_url'] = base_url('pagintion2/manage');
        $config['total_rows'] = $this->db->count_all('tbl_books');
        $config['per_page'] = $per_page;
        $choice = $config["total_rows"]/$config["per_page"];
        $config["num_links"] = floor($choice);
        $config['use_page_numbers'] = TRUE;
        $config['page_query_string'] = TRUE;
        $config['uri_segment'] = 3;
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['first_link'] = false;
        $config['last_link'] = false;
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['prev_link'] = '«';
        $config['prev_tag_open'] = '<li class="prev">';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = '»';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        // CodeIgniter Pagination URL with GET Parameters
        // http://subhra.me/codeigniter-pagination-url-get-parameters/
        if (count($_GET) > 0) $config['suffix'] = '?' . http_build_query($_GET, '', "&");
        $config['first_url'] = $config['base_url'].'?'.http_build_query($_GET);

        $this->pagination->initialize($config);
        $data['pagination_links'] = $this->pagination->create_links();   
        // echo $data['pagination_links'];   
        $data['templatepart']='backend/pagination2';
        $userdata= $this->session->userdata('userdata');
        $data['data']= $this->Admin_model->getdata('user',array('id'=>$userdata['id']));
        $this->load->view('backend/layout',$data);
   }
}

问题:

当我生成查询字符串时,例如。 ?per_page = 2给我一个致命的错误,并且无法操纵查询字符串。

例如:

localhost/xxx/pagintion2/manage?per_page=2&book_name=Learning

错误:

404 Page Not Found

请给我一些解决方案。如果您有重新编码的代码,请与我分享。

问候

1 个答案:

答案 0 :(得分:0)

1.创建用于保存查询的SQL表

        --
        -- Table structure for table `ci_query`
        --

        CREATE TABLE `ci_query` (
          `id` int(11) NOT NULL,
          `query_string` text,
          `date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
        ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
    文件夹“application / core”中的
  1. cerate文件:My_input.php

    if (!defined('BASEPATH')) exit('No direct access allowed.');
    
    
    /**
     * Description of My_input
     *
     * @author Dragan
     */
    class MY_Input extends CI_Input{
    
        function __construct() {
            parent::__construct();
        }
    
        /**
        * Save query in database
        */
        function save_query($query_array){
            $CI =& get_instance();
            $CI->db->insert('ci_query', array('query_string' => http_build_query($query_array)));
    
            return $CI->db->insert_id();    
        }
    
        /**
        * Load query from database
        */
        function load_query($query_id) {
    
            $CI =& get_instance();
    
            $rows = $CI->db->get_where('ci_query', array('id' => $query_id))->result();
            if (isset($rows[0])) {
                parse_str($rows[0]->query_string, $_GET);       
            }
    
        }
    
    
        }
    
  2. 在您的Controller cerate功能

        public function search() {
    
                    $query_array = array(
                        'book_name' => $this->input->post('book_name')
                    );
    
                    $query_id = $this->input->save_query($query_array);
                    redirect("pagintion2/manage/$query_id");
    
                }
    
  3. 更改您的管理功能

     public function manage ($query_id = 0,$start=0){
    
                $this->input->load_query($query_id);  
    
                $query_array = array(
                    'book_name' => $this->input->get('book_name')
                );
    
                $per_page = 10;
    
                $search= $this->input->get('book_name')?$this->input->get('book_name'):'NIL';
                 if($search=='NIL')
               {
                $data['content_fb'] = $this->db->get('tbl_books')->result();
               }
               else
               {
                $this->db->like('name', $search);
                $res = $this->db->get('tbl_books');
                $data['content_fb'] =  $res->result();
               }
    
    
                $this->load->library('pagination');
                $config['base_url'] = site_url()."pagintion2/manage/$query_id";
                $config['total_rows'] = $this->db->count_all('tbl_books');
                $config['uri_segment'] = 4;
                $config['per_page'] = 10;
                $config['full_tag_open'] = '<ul class="pagination">';
                $config['full_tag_close'] = '</ul>';
                $config['first_link'] = false;
                $config['last_link'] = false;
                $config['first_tag_open'] = '<li>';
                $config['first_tag_close'] = '</li>';
                $config['prev_link'] = '«';
                $config['prev_tag_open'] = '<li class="prev">';
                $config['prev_tag_close'] = '</li>';
                $config['next_link'] = '»';
                $config['next_tag_open'] = '<li>';
                $config['next_tag_close'] = '</li>';
                $config['last_tag_open'] = '<li>';
                $config['last_tag_close'] = '</li>';
                $config['cur_tag_open'] = '<li class="active"><a href="#">';
                $config['cur_tag_close'] = '</a></li>';
                $config['num_tag_open'] = '<li>';
                $config['num_tag_close'] = '</li>';
                $this->pagination->initialize($config);
    
                $data['pagination_links'] = $this->pagination->create_links();
    
               // output search data
               $data['list_of_books_by_category'] =  $this->model_your_model_name->get_list_of_books_by_category($query_array, $per_page, $start);
    
                $data['templatepart']='backend/pagination2';
                $userdata= $this->session->userdata('userdata');
                $data['data']= $this->Admin_model->getdata('user',array('id'=>$userdata['id']));
                $this->load->view('backend/layout',$data);
    
            }