CI分页链接在搜索后不起作用

时间:2016-06-27 00:59:21

标签: php codeigniter pagination

我现在正在使用codeigniter分页搜索。我的分页工作正常,但问题是在搜索后触发的。分页链接显示,但在我点击下一页或第二页后,它不会进入下一页。

这就是我现在所拥有的。

控制器

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Division extends MY_Controller {

function __construct(){

    parent::__construct();
    $this->load->model('division_model');
    $this->load->helper('url');
    $this->load->library('table');
    $this->load->library("pagination");
}

public function index() {
    $this->data['title'] = 'Division List'; 
    $this->data['result'] = $this->search();
    $this->middle = 'division_view';
    $this->layout();
}

function search(){

    $search = $this->input->get("search");
    $config = array();
    $config["base_url"] = base_url() . "division/$search";
    $config['total_rows'] = $this->division_model->record_count($search);
    $config["per_page"] = 10;
    $config["uri_segment"] = 2;
    $choice = $config["total_rows"] / $config["per_page"];
    $config['full_tag_open'] = '<ul class="pagination"><li>';
    $config['full_tag_close'] = '</li></ul>';
    $config['cur_tag_open'] = '<li class="active"><a href="'.base_url().$this->uri->uri_string().'">';
    $config['cur_tag_close'] = '</a></li>';
    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';
    $config['next_tag_open'] = '<li>';
    $config['next_tag_close'] = '</li>';
    $config['last_tag_open'] = '<li>';
    $config['last_tag_close'] = '</li>';
    $config['use_page_numbers'] = TRUE;

    $this->pagination->initialize($config);
    //echo $this->pagination->create_links();
    $pagination = $this->uri->segment(2);
    if($pagination == "") { $pagination = 0; }
    $offset = $pagination==0? 0: ($pagination-1)*$config["per_page"];

    $data['results'] = $this->division_model->search_page($config["per_page"], $offset, $search);
    $data['links'] = $this->pagination->create_links();

return $data;
}

}

模型

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Division_model extends CI_Model {

function record_count($search = NULL) {
    //if ($search == "NIL") $search = "";
    $query = $this->db->like('division_name',$search)->or_like('division_code',$search)->or_like('division_acro',$search)->get("division");
    return $query->num_rows();
}

function search_page($limit, $start, $search = NULL) {
    $this->db->like('division_name',$search)->or_like('division_code',$search)->or_like('division_acro',$search)->limit($limit, $start);
    $query = $this->db->get("division");
    if ($query->num_rows() > 0) {
        foreach ($query->result() as $row) {
        $data[] = $row;
    }
        return $data;
    }
    return false;

}

任何帮助都会表示赞赏。

3 个答案:

答案 0 :(得分:0)

检查此行....

$ config [“base_url”] = base_url()。 “分割/ $搜索”;

应该是

$ config [“base_url”] = base_url()。 “分割/搜索”;

答案 1 :(得分:0)

修复如下:

$config["base_url"] = base_url("division/search");

// your offset will be in segment 3 --> division/search/offset
$config["uri_segment"] = 3; 

// translate number of page to offset so it will be something like
// division/search/10 division/search/20 division/search/30 etc rather than
// division/search/1 division/search/2 division/search/3 etc
$config['use_page_numbers'] = FALSE; 

希望这有帮助:)

答案 2 :(得分:0)

试试这个。更改$config数组的最后4行,然后更改$pagination变量

 $config["base_url"] = site_url('division/search?search='.$search);
$config['total_rows'] = $this->division_model->record_count($search);
$config["per_page"] = 10;
$config["uri_segment"] = 2;
$choice = $config["total_rows"] / $config["per_page"];
$config['full_tag_open'] = '<ul class="pagination"><li>';
$config['full_tag_close'] = '</li></ul>';
$config['cur_tag_open'] = '<li class="active"><a href="'.base_url().$this->uri->uri_string().'">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['enable_query_strings'] = TRUE;
$config['use_page_numbers'] = TRUE;
$config['query_string_segment'] = 'page';
$config['page_query_string'] = TRUE;

$this->pagination->initialize($config);

if ($this->input->get('page')) {
        $sgm = (int) trim($this->input->get('page'));
        $segment = $config["per_page"] * ($sgm - 1);
    } else {
        $segment = 0;
    }
$data['results'] = $this->division_model->search_page($config["per_page"], $segment, $search);