如果我点击下一个数字链接,我对我的观点有分页效果很好。
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>
请看问题在哪里。
答案 0 :(得分: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');
}
答案 1 :(得分: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);
}
视图文件中的代码段显示在上面