【问题标题】:How do I add pagination to codeigniter如何将分页添加到 codeigniter
【发布时间】:2018-07-20 17:53:33
【问题描述】:

以下是 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>

谢谢

【问题讨论】:

标签: codeigniter pagination


【解决方案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();

【讨论】:

    【解决方案2】:

    查看示例如何添加分页。

    让我的控制器是 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();
     ?>
    

    【讨论】:

      猜你喜欢
      • 2016-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多