1

如果我点击下一个数字链接,我的视图有分页,它工作正常。比如:

1 2 > 

因此,如果我单击 2,它将进入第二页并显示正确的结果。但现在如果我想返回 1,而不是重定向我。即使有链接 3,我也无法访问 2。

这是我的代码:

控制器:

function list_providers()
{

    $admin_session_data= $this->session->userdata('user_logged_in');

    $this->load->model('/admin/provider_model');
    $this->load->library('pagination');
    $this->load->helper('url');

    $config['base_url'] = base_url()."index.php/admin/provider/list_providers/";
    $config['total_rows'] = $this->provider_model->countprovider();
    $config['per_page'] = 3;
    $config['creat_link'] = 1;
    $config['first_link'] = 'First';
    $config['last_link'] = 'Last';

    $this->pagination->initialize($config);
    $offset=($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
    $data['list']= $this->provider_model->listproviders($config['per_page'],$offset);

    //$data['customer']= $this->provider_model->getcustomer_data($config['per_page'],$this->uri->segment(3));


    if(!empty($data))
    {   
        $this->load->view('admin/admin_header',$admin_session_data); 
        $this->load->view('admin/admin_top_nav',$admin_session_data); 
        $this->load->view('admin/admin_left',$admin_session_data);
        $this->load->view('admin/provider_list',$data);
        $this->load->view('admin/admin_footer',$admin_session_data);
    }

}

模型:

function listproviders($num,$offset)
{
    $this->load->library('pagination');

    //$query=$this->db->get_where('users', array('user_role_id'=>'3'));
    $query=$this->db->get('users',$num,$offset);

    // return result set as an associative array
    return $query->result_array();
}


function countprovider()
{
    $this->load->library('pagination');
    $query=$this->db->count_all_results('users');
    return $query;
}

我的分页显示如下: 1 2 >

请看问题出在哪里。

4

2 回答 2

3

我对您的控制器和模型进行了一些更改。更改评论:

控制器

// admin/provider/list_providers/3 <- Offset number
function list_providers($offset = null)
{    
    $admin_session_data= $this->session->userdata('user_logged_in');

    // Load models/libraries/helpers
    $this->load->model('admin/provider_model');
    $this->load->library('pagination');
    $this->load->helper('url');

    // Pagination config
    $config['base_url'] = site_url('index.php/admin/provider/list_providers') . '/';
    $config['total_rows'] = $this->provider_model->countprovider();
    $config['per_page'] = 3;
    // $config['creat_link'] = 1; <-- This is not a valid option
    $config['first_link'] = 'First';
    $config['last_link'] = 'Last';

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

    // If the pagination library doesn't recognize the current page add:
    $this->pagination->cur_page = $offset;

    $data['list']= $this->provider_model->listproviders($config['per_page'],$offset);

    if(!empty($data))
    {   
        $this->load->view('admin/admin_header',$admin_session_data); 
        $this->load->view('admin/admin_top_nav',$admin_session_data); 
        $this->load->view('admin/admin_left',$admin_session_data);
        $this->load->view('admin/provider_list',$data);
        $this->load->view('admin/admin_footer',$admin_session_data);
    }
}

模型

function listproviders($num,$offset)
{
    // $this->load->library('pagination'); <-- There is no need for this
    $query = $this->db->get('users', $num, $offset);

    // Check if pages where found
    if ($query->num_rows() > 0)
    {
        return $query->result_array();
    }
    else
    {
        return FALSE;
    }

}


function countprovider()
{
    // $this->load->library('pagination'); <-- There is no need for this
    return $this->db->count_all_results('users');
}
于 2012-09-15T11:02:02.973 回答
1

直接从库中创建链接.. 像这样..

$data['links']= $this->pagination->create_links();

在视图中给出类似的东西

<div align="center" style="margin-top:10px;background-color: #F1F8E0">
       <?php echo $links; ?>
</div>

编辑:

示例代码:

function  __construct()
    {

        parent::__construct();
        $this->load->helper('array');
        $this->load->helper('url');
        $this->load->helper('html');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->library('pagination');
        $this->load->library('session');
        $this->load->model('listing','',TRUE);
    }

    // Shows pagination

    function index($uri_segment="3")
{   
        $config['base_url'] = base_url('index.php/listing_controller/index');
        $config['total_rows'] = $this->listing->get_rows();
        $config['per_page'] = 5;
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $output['op']=$this->listing->get_paging_data($config['per_page'],$this->uri->segment(3));
        $output['links']= $this->pagination->create_links();
        $this->load->view('listing_view',$output);
    }

视图文件中的片段如上所示

于 2012-09-15T10:48:28.063 回答