在Codeigniter中没有显示分页

时间:2018-06-09 15:51:47

标签: php codeigniter pagination

我正在处理CodeIgniter项目,但分页没有显示在我的视图文件中。 我自动加载了分页库 我在数据库中共有7行,& per_page设置5行。我还会检查print_r($config)&它显示数据很好。

这是我的控制者:

public function cust_list()
{
    $this->load->model('Cus_model');
    $config =
    [
        'base_url'=>base_url('index.php/Cus_controller/cust_list'),
        'per_page'=>5,
        'totle_rows'=>$this->Cus_model->num_rows(),    
    ];
    $this->pagination->initialize($config);
    $customer=$this->Cus_model->customer_list($config['per_page'],$this->uri->segment(4));
    $this->load->view('salonadmin/customerview',['customers'=>$customer]);
 }

这是我的Cus_model

  public function customer_list($limit,$offset)
  {
  $user_id=$this->session->userdata('user_id');
  $query=$this->db
  ->select('*')
  ->from('customer')
  ->limit( $limit,$offset )
  ->get();
  return $query->result();
  }
  public function num_rows()
  {
  $user_id=$this->session->userdata('user_id');
  $query=$this->db
  ->select('*')
  ->from('customer')

  ->get();

  return $query->num_rows();

  }

&安培;这是我的观点:

<?= $this->pagination->create_links();?>

1 个答案:

答案 0 :(得分:3)

控制器

 public function cust_list() {
   $this->load->model('cus_model');
   $config["base_url"] = base_url() . "cus_controller/cust_list/";
   $config["total_rows"] = $this->cus_model->num_rows();
   $config["per_page"] = 5;
   $config['cur_tag_open'] = '&nbsp;<a class="current">';
   $config['cur_tag_close'] = '</a>';
   $config['next_link'] = 'Next';
   $config['prev_link'] = 'Previous';
   $this->pagination->initialize($config);

   // You should change your parameter according your url segment like 3 / 4
   if($this->uri->segment(3)){
      $page = $this->uri->segment(3);
   }else{ 
      $page = 0; 
   }

   $this->data["customers"] = $this->cus_model- 
   >customer_list($config["per_page"], $page);
   $str_links = $this->pagination->create_links();
   $this->data["links"] = explode('&nbsp;',$str_links );

   $this->load->view('salonadmin/customerview',$this->data);
}

模型

public function customer_list($limit,$offset){
  $query = $this->db->select('*')->from('customer')->limit( $limit,$offset 
  )->get()->result();
  return $query;
}

public function num_rows()
{
  $query = $this->db->select('*')->from('customer')->get()->num_rows();
  return $query;
}

查看

<-- Pagination Link -->
<ul class="pagination pull-right">
  <?php foreach ($links as $link) {
    echo "<li>". $link."</li>";
  } ?>
</ul>