【问题标题】:CodeIgniter - How do I do Pagination?CodeIgniter - 我如何进行分页?
【发布时间】:2018-09-06 07:42:45
【问题描述】:

我查看了很多关于分页的问题,但我无法真正理解分页的工作原理。
我需要分页来处理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 新手,这只是我的测试页面,请帮助,谢谢。

【问题讨论】:

    标签: php html twitter-bootstrap codeigniter


    【解决方案1】:

    您的控制器应如下所示:

    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();
    
     }
    

    【讨论】:

    • 我需要修改我的视图吗?
    • 它在我的控制器上有`未定义的属性:EstoreReport::$orders`
    • 以及控制器中的Call to a member function get_orders() on a non-object
    【解决方案2】:

    检查这些:

    codeigniter pagination

    codeigniter pagination 2

    我已经在 StackOverflow 上的 Codeigniter 中给出了分页问题的答案。这些链接有完整的描述。希望它会有所帮助

    【讨论】:

    • 我不了解模型部分。我应该如何修改它
    • 你检查过上面给出的codeigniter pagination 2链接吗?
    • $qr = $this&gt;db-&gt;get('TABLENAME', $per_page, $this-&gt;uri-&gt;segment(3)); 这是什么意思?
    • 它正在设置限制和偏移量。 $per_page 具有限制值,即您在控制器中的 $config 中定义的值,第三个参数用于传递偏移量,即要跳过多少行,如前几页所示
    • 您可以通过在此行后面加上echo $this-&gt;db-&gt;last_query(); 来理解这一点,以了解此行正在生成什么查询。
    猜你喜欢
    • 2016-01-08
    • 1970-01-01
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多