首页 > 解决方案 > CodeIgniter - 我如何进行分页?

问题描述

我浏览了很多关于分页的问题,但我不能真正理解分页是如何工作的。当用户输入日期范围时,
我需要分页。在我的控制器中:index()searchdate()

public function __construct() {
  parent::__construct();
  $this->load->model('ReportModel');
}

public function index()
{
   $orders=new ReportModel;
   $data['data']=$orders->get_orders();
   $this->load->view('includes/header');
   $this->load->view('Report/view',$data);
   $this->load->view('includes/footer');
}

public function searchDate()
{
   $orders=new ReportModel;
   $searchfrom = $this->input->post('searchDateFrom');
   $searchto = $this->input->post('searchDateTo');
   $data['data']=$orders->get_orders($searchfrom,$searchto);

   $this->load->view('includes/header');
   $this->load->view('Report/view',$data);
   $this->load->view('includes/footer');
}


在我的模型中:

 public function get_orders(){
    $searchDateFrom = $this->input->post("searchDateFrom");
    $searchDateTo = $this->input->post("searchDateTo");
    $this->db->select('platform,id,no,date,printed_date');
    $this->db->from('orders');

    if(!empty($searchDateFrom) || !empty($searchDateTo) || !empty($searchPlatform)){
        if (!empty($searchDateFrom)) {
            $this->db->where('date >= ', $this->input->post("searchDateFrom"));
        }
        if (!empty($searchDateTo)) {
            $this->db->where('date <= ', $this->input->post("searchDateTo")." 23:59:59");
        }          
    }

    $this->db->order_by("date", "desc");
    $this->db->limit(300);

    $query = $this->db->get();

    return $query->result();
 }


在我看来:

<div class="pull-right">
        <form class="form-inline" role="search" action="<?php echo base_url('estoreReport/searchDate')?>" method = "post">
            <div class="form-group">
              <input type="date" class="form-control" placeholder="Order Date From" name = "searchDateFrom" ">
              <input type="date" class="form-control" placeholder="Order Date To" name = "searchDateTo" ">                    
            </div>
            <button class="btn btn-default " type="submit" value = "searchDateTo"><i class="glyphicon glyphicon-search"></i>
        </form>
      </div>
</div>

<div class="table-responsive">
<table class="table table-bordered">
  <thead>
      <tr>
          <th>Platform</th>
          <th>ID</th>
          <th>No</th>
          <th>Date</th>
          <th>Printed Date</th>
      </tr>
  </thead>
  <tbody>
   <?php foreach ($data as $d) { ?>
      <tr>
      <td ><?php echo $d->platform; ?></td>
      <td><?php echo $d->id; ?></td>
      <td><?php echo $d->no; ?></td>
      <td><?php echo $d->date; ?></td>
      <td><?php echo $d->printed_date; ?></td>
      <td></td>
      <td></td>
      </tr>
      <?php } ?>
  </tbody>
</table>
</div>

我已经阅读了 CodeIgniter 文档,并且知道我应该将配置放入控制器中。

 public function pagination($count){
     $this->load->library('pagination');

     $config['base_url'] = base_url('/order/Report/');
     $config['total_rows'] = $count;
     $config['per_page'] = 100;
     $config["uri_segment"] = 3;
     $choice = $config["total_rows"] / $config["per_page"];
     $config["num_links"] = ;
     $config['use_page_numbers'] = TRUE;
     $config['reuse_query_string'] = TRUE;
     $page = ($this->uri->segment($config["uri_segment"] )) ? $this->uri->segment($config["uri_segment"] ) : 0;

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

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

     return array($page, $config['per_page'], $pagination);
 }

但我仍然不确定如何修改控制器、模型和视图的其他部分。我是这里的 CodeIgniter 新手,这只是我的测试页面,请帮助,谢谢。

标签: phphtmltwitter-bootstrapcodeigniter

解决方案


您的控制器应如下所示:

public function __construct() {
  parent::__construct();
  $this->load->model('ReportModel', 'orders');
  $this->load->library('pagination');
}

public function index($offset = 0)
{
    $data['data']=$this->orders->get_orders($search = array(), $offset);
    $config['total_rows'] = $data['total_rows'] = $this->orders->get_orders($search = array(), $offset, true);
    $config['base_url'] = base_url('/order/index/');
    $config['per_page'] = 100;
    $config["uri_segment"] = 3;
    $config['reuse_query_string'] = TRUE;
    $this->pagination->initialize($config);

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

   $this->load->view('includes/header');
   $this->load->view('Report/view',$data);
   $this->load->view('includes/footer');
}

然后您的模型应该更改为也能够对结果进行分页:

public function get_orders($search = array(), $offset = 0, $count = false){
    $searchDateFrom = $search["searchDateFrom"];
    $searchDateTo = $search["searchDateTo"];
    $this->db->select('platform,id,no,date,printed_date');
    $this->db->from('orders');

    if(!empty($searchDateFrom) || !empty($searchDateTo) || !empty($searchPlatform)){
        if (!empty($searchDateFrom)) {
            $this->db->where('date >= ', $this->input->post("searchDateFrom"));
        }
        if (!empty($searchDateTo)) {
            $this->db->where('date <= ', $this->input->post("searchDateTo")." 23:59:59");
        }          
    }

    $this->db->order_by("date", "desc");
    if ( !$count ) {
        $this->db->limit(100, $offset);
        $query = $this->db->get();
        return $query->result();
    }
    $query = $this->db->get();
    return $query->num_rows();

 }

推荐阅读