Codeigniter分页工作但有一些问题

时间:2015-12-07 07:53:15

标签: php codeigniter

基于上面的标题,我在codeigniter中有一个分页,它在第一页上显示我想要的内容。但是当我点击第二页时,它显示的与第一页相同。任何帮助将不胜感激。

型号:

public function get_users ($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get ('auth_users');
$result = $query->result ();
  foreach ($result as $key => $value) {
  $data[$key] = array(
      'id'  => $value->id,
      'firstname' => $value->first_name,
      'lastname' => $value->last_name,
      'datereg' => $value->date_registered,
      'email' => $value->email,
      'status' => $value->status ? 'Active' : 'Inactive'
  );
}
return $data;

}

控制器:

 public function users_page () {
    // Pagination
$config['base_url'] = site_url('main/index');
$config['total_rows'] = $this->db->count_all('auth_users');
$config['per_page'] = 5;
$config['uri_segment'] = 3;
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links"] = round($choice);

// Config pagination for bootstrap
$config['full_tag_open'] = '<ul class="pagination pagination-sm">';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = false;
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['last_link'] = false;
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['next_link'] = '&raquo';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['prev_link'] = '&laquo';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';

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

$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;      
$data['users'] = $this->user->get_users($config["per_page"], $page); 
$data['pagination'] = $this->pagination->create_links();

// $data['users'] = $this->user->get_users();
$data['title'] = 'Exer-Claide | Members area';

$this->load->view('fragments/header', $data);
$this->load->view('fragments/footer');
$this->load->view ('user_page' , $data);

}

2 个答案:

答案 0 :(得分:1)

试试这个

替换

$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

$p = isset($_GET['page']) ? $_GET['page'] : 0;
$page = ($p) ? $p : 0; 

答案 1 :(得分:0)

检查$this->uri->segment(3)的值。例如

echo $this->uri->segment(3);

这可能不会给出适当的价值。如果正确,每当刷过页面时都会给你另一个值。

示例:

$this->uri->segment(3) = 1

下一页

$this->uri->segment(3) = 2

如果没有给出不同的值,请更改值。

示例:

$this->uri->segment(3) or $this->uri->segment(2)

函数$ this-&gt; uri-&gt; segment()返回确定位置的url值。