代码点火器 - 如何进行分页?



我已经看了很多关于分页的问题,但我真的不明白分页是如何工作的。
我需要分页来处理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 学习者,这只是我的测试页面,请帮忙,谢谢。

您的控制器应如下所示:

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

检查这些:

代码点火器分页

编码点火器分页 2

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

相关内容

  • 没有找到相关文章

最新更新