我正在使用CI分页。在主页上显示正确的页数及其链接但是当我点击下一页上的任何页面时,链接就会消失。这是我的代码,我正在使用搜索,一起排序功能
控制器
function projects($order_by="project_id", $sort_order="DESC", $term=0, $page=0, $rows=2){
if(!isMemberLoggedIn()){
redirect('admin/login');
}
else{
$member_id = "";
if(!isSuperAdmin()){
$member_id = $this->session->userdata('member_id');
}
if(isset($_POST['term'])){
$term = $this->input->post('term');
}
$data['term'] = $term;
$data['order_by'] = $order_by;
$data['sort_order'] = $sort_order;
$this->load->model('project_model', '', TRUE);
$data['projects'] = $this->project_model->getProjects(array('member_id'=>$member_id, 'term'=>$term, 'order_by'=>$order_by, 'sort_order'=>$sort_order, 'rows'=>$rows, 'page'=>$page));
//echo '<pre>';print_r($data['projects']);die;
//total number of projects
$data['total'] = $this->project_model->getProjects(array('member_id'=>$member_id, 'term'=>$term, 'order_by'=>$order_by, 'sort_order'=>$sort_order, 'rows'=>$rows, 'page'=>$page),true);
$data['from_projects'] = $page;
$this->load->library('pagination');
$config['base_url'] = site_url('admin/projects/'.$order_by.'/'.$sort_order.'/'.$term);
$config['total_rows'] = $data['total'];
$config['per_page'] = $rows;
$config['uri_segment'] = 5;
$config['first_link'] = 'First';
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['last_link'] = 'Last';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['full_tag_open'] = '<div class="pagination"><ul>';
$config['full_tag_close'] = '</ul></div>';
$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>';
$config['prev_link'] = '«';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '»';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$this->pagination->initialize($config);
$this->load->view('admin/projects',$data);
}
}
和模型
function getProjects($data=array(), $return_count=false)
{
if(empty($data))
{
//if nothing is provided return the whole shabang
$this->getAllProjects();
}
else
{
$this->db->join('members','members.member_id=projects.member_id');
if(!empty($data['member_id']))
{
$this->db->where('members.member_id',$data['member_id']);
}
//grab the limit
if(!empty($data['rows']))
{
$this->db->limit($data['rows']);
}
//grab the offset
if(!empty($data['page']))
{
$this->db->offset($data['page']);
}
//do we order by something other than category_id?
if(!empty($data['order_by']))
{
//if we have an order_by then we must have a direction otherwise KABOOM
$this->db->order_by($data['order_by'], $data['sort_order']);
}
//do we have a search submitted?
/****if(!empty($data['term']))
{
$search = json_decode($data['term']);
//if we are searching dig through some basic fields
if(!empty($search->term))
{
$this->db->like('name', $search->term);
$this->db->or_like('hover_name', $search->term);
$this->db->or_like('description', $search->term);
$this->db->or_like('excerpt', $search->term);
$this->db->or_like('sku', $search->term);
}
if(!empty($search->category_id))
{
//lets do some joins to get the proper category products
$this->db->join('category_products', 'category_products.product_id=products.id', 'right');
$this->db->where('category_products.category_id', $search->category_id);
$this->db->order_by('sequence', 'ASC');
}
}****/
if($return_count)
{
return $this->db->count_all_results('projects');
}
else
{
return $this->db->get('projects')->result();
}
}
}
最后一个观点如下
<?php echo $this->pagination->create_links();?>
请帮助我们如何解决这个问题。
提前致谢!
答案 0 :(得分:0)
当没有要显示的分页时,create_links()函数返回一个空字符串。
Pagination Class : Codeigniter
更新回答:
似乎$config['total_rows'] = $data['total'];
$config['total_rows']
=&gt;此数字表示您为其创建分页的结果集中的总行数。通常,此数字将是数据库查询返回的总行数。
在您的情况下,$config['total_rows']
等于要在当前页面上显示的行。
$data['total'] = $this->project_model
->getProjects(
array(
'member_id'=>$member_id,
'term'=>$term,
'order_by'=>$order_by, /* Remove this */
'sort_order'=>$sort_order, /* Remove this */
'rows'=>$rows, /* Remove this */
'page'=>$page) /* Remove this */
,true);
删除提到的数组索引。
计算total_rows不需要 Sort order
和order by
。它没有任何意义。 $page
和$rows
用于限制在当前页面上显示的行。这两个参数不会让您得到准确的total_rows
。