以下是ci控制器的外观。 我只是关注Youtube频道的教程。 如何添加分页以使页面每页仅加载20个结果?
<?php
class Product_details extends CI_Controller{
function index(){
$this->load->library('pagination');
$this->load->model('product_model');
$data['userArray'] = $this->product_model->return_products();
$this->load->view('product_listing',$data);
}
}
查看
<table>
<tr>
<th>ID</th>
<th>Product Name</th>
<th>Product Price</th>
<th>Product Image</th>
</tr>
<?php
foreach ($userArray as $key => $value) {
echo "<tr>
<td>".$value['id']."</td>
<td>".$value['post_id']."</td>
<td>".$value['price']."</td>
<td><img src=".$value['imageUrl']."/></td>
</tr>";
}
?>
</table>
谢谢
答案 0 :(得分:1)
您可以在模型中传递页码。
控制器
<?php
class Product_details extends CI_Controller{
function index($pageNo){
$this->load->model('product_model');
$data['userArray'] = $this->product_model->return_products($pageNo);
$this->load->view('product_listing',$data);
}
}
模型
public function all($pageNo){
$pageNo -= 1;
$this->db->select("*")
->from('products')
->order_by('id',"ASC")
->limit(20, $pageNo * 20);
$query = $this->db->get();
return $query->result_array();
答案 1 :(得分:0)
请参阅示例如何添加分页。
让我的控制器是Dashboard.php,它的方法是索引。 现在配置分页。
public function index()
{
$config['base_url'] = site_url('/dashboard/index/');
$config['total_rows'] = $this->dashboard_model->num_rows();
$config['per_page'] = 5;
$config['use_page_numbers'] = FALSE;
$this->pagination->initialize($config);
$data['data'] = $this->dashboard_model->all_blog($config['per_page'], $this->uri->segment(3));
$this->load->view("/dashboard/blog", $data);
}
模型是Dashboard_model.php
public function all_blog($limit, $offset)
{
$this->db->from('blogs');
$this->db->select('*');
$this->db->order_by("created_at", "desc");
$q = $this->db->get();
$data = $q->result_array();
return $data;
}
public function num_rows()
{
$this->db->from('blogs');
$this->db->select('*');
$this->db->order_by("created_at", "desc");
$this->db->limit($limit, $offset);
$q = $this->db->get();
$data = $q->num_rows();
return $data;
}
现在是我的观点blog.php
<div class="table-responsive">
<table class="footable table table-stripped toggle-arrow-tiny" data-page-size="8" data-filter=#filter; id="filter">
<thead>
<tr>
<th></th>
<th>S.No</th>
<th>Title </th>
<th>Blog Image</th>
<th>Added For</th>
</tr>
</thead>
<tbody>
<?php
$count = $this->uri->segment(3, 0);
foreach ($data as $key) :
?>
<tr>
<td><input type="checkbox" class="i-checks" name="input[]"></td>
<td><?php echo ++$count; ?></td>
<td><?php echo $key['blog_title']; ?></td>
<td><img src="<?php echo $key['blog_image']; ?>" style="width: 150px; height: 80px" /></td>
<td><?php echo $key['user_type']; ?></td>
</tr>
<?php endforeach?>
</tbody>
</table>
</div>
<?php
echo $this->pagination->create_links();
?>