【问题标题】:CodeIgniter 3 pagination not workingCodeIgniter 3分页不起作用
【发布时间】:2018-05-30 08:55:11
【问题描述】:

我重温了 CodeIgniter 的分页类。

由于文档缺乏详细信息,我在网上查看了许多教程。

所以我复制粘贴了教程并更改了一些相关细节以适应我拥有的随机表。

控制器:

public function sample_pagination($offset = 0){
    $num_rows=$this->db->count_all("genre");
    $config['base_url'] = base_url().'pages/sample_pagination/';
    $config['total_rows'] = $num_rows;
    $config['per_page'] = 5;
    $config['num_links'] = $num_rows;
    $config['use_page_numbers'] = TRUE;
    $this->pagination->initialize($config);

    $data['records']=$this->db->get('genre', $config['per_page'],$offset);// take record of the table
    $header = array('genre_Id','name'); // create table header
    $this->table->set_heading($header);// apply a heading with a header that was created
    $this->load->view('pages/sample',$data); // load content view with data taken from the users table
    }

观点:

<body>

<div id="container">
<h1>Welcome to CodeIgniter Pagination System!</h1>

<div id="body">



<?php echo $this->table->generate($records); ?>
<?php echo $this->pagination->create_links(); ?>
</div>

<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
</div>

</body>

它工作正常,我唯一的问题是,我拥有的数据库表类型有 14 个条目,视图上的结果因每页设置而异。

例如上面的例子,我将 per_page 设置为 5。它将显示分页的每页 5 行,带有 3 个分页链接

问题是细节只显示到表格的第 8 行,有 14 行,并且重复。

首页是

1 2 3 4 5

第二页是

3 4 5 6 7

第三页是

4 5 6 7 8

我指的是上面的genre_id。我的表流派将流派 ID 作为 PK,流派名称作为列。所以它只有 2 列。

无论如何,如果我将 Per_page 设置为 1,它会正确显示从genre_id 1 到 14 的所有表行。

我很困惑这是怎么回事?我自己做很容易学习,所以我尝试通过在线复制教程来做,这样我就可以看到它是如何工作的,但我对此感到困惑。

【问题讨论】:

  • sample_pagination 控制器对吗?并且您将页码作为偏移量传递?你不是那样做的。
  • 它来自我在网上找到的教程。 w3programmers.com/… 和 'pages' 是控制器,而 'sample_pagination' 是方法。
  • 我想我开始明白为什么会这样了。在 codeigniter 活动记录中,您检索的记录有一个偏移量/限制,并且本教程使用偏移量的方式一定与 db 查询混淆了?例如 $this->db->get('table', 1);只会检索 1 行。我从未将两者联系起来,因为我不知道偏移量是正确还是错误,但看到它是一个教程,我只是假设它是正确的。
  • 我想是的,我现在看不懂教程,但我会用我使用的代码写一个答案

标签: php codeigniter pagination


【解决方案1】:

在控制器文件中:

public function favourite()
{
  $this->load->library('pagination'); // load pagination library
  $limit_per_page = 5;
  $offset = ($this->input->get('offset')) ? $this->input->get('offset'):0 ;  // actually codeigniter return offset not page 
  $total_records = $this->users->count_favorites($id);
   if ($total_records > 0)
    {
      $data['favorites']=$this->users->favoritesProducts($id,$limit_per_page, $offset);
      $config['base_url'] = base_url() . 'user/favourite';
        $config['total_rows'] = $total_records;
        $config['per_page'] = $limit_per_page;
        $config['enable_query_strings']=TRUE;
        $config['query_string_segment'] = 'offset';
        $config['page_query_string'] = TRUE;

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

        // build paging links
        $data["links"] = $this->pagination->create_links();
       }
       $data['viewfile'] = 'user/favourite';
      $this->load->view('template', $data);

    }

查看favourite.php文件简单显示链接变量Like

<?php
      if(isset($links)) {
     echo $links; 
    }
   ?>

在模型文件Users.php中

       public function count_favorites($user_id){  
          $this->db->from('favorites f');   
         $this->db->where("user_id",$user_id);
        $this->db->order_by("f.id","DESC");
        $result=$this->db->count_all_results();
        return $result;      
       } 


      public function favoritesProducts($user_id,$limit=2, $start=0){
         $this->db->select('p.name,f.id,p.product_id,p.sku,p.small_image');    
        $this->db->from('favorites f'); 
        $this->db->join('products p', 'f.product_id=p.product_id');
        $this->db->where("f.user_id",$user_id);
        $this->db->order_by("f.id","DESC");
        //$this->db->where('site_id',SITE_ID);
           $this->db->limit($limit, $start);
        $query = $this->db->get();
        $result=$query->result();
        return $result;
       }

而且运行良好! 网址喜欢:http://localhost//user/favourite?offset=5

【讨论】:

    【解决方案2】:

    这样做

    function sample_pagination()
    {
        $num_rows=$this->db->count_all("genre");
        $config['base_url'] = base_url().'pages/sample_pagination/';
        $config['total_rows'] = $num_rows;
        $config['per_page'] = 5;
        $config['uri_segment'] = 2; # added - 
        $config['num_links'] = $num_rows;
        $this->pagination->initialize($config);
    
        $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
    
        $data['records']=$this->db->get('genre', $config['per_page'],$page);
    
        /*No Tested below this*/
        $header = array('genre_Id','name');
        $this->table->set_heading($header);
        $this->load->view('pages/sample',$data);
    }
    

    确保页面加载时没有index.php 并且段正确。

    【讨论】:

    • 这条线是什么? $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
    • 这就是CI现在在哪个页面分页
    【解决方案3】:

    我只是删除了这个:或将其设置为 FALSE。

    $config['use_page_numbers'] = TRUE;
    

    在阅读了 2 个答案后,我开始明白让我的分页混乱的是偏移量。由于我将使用页码设置为 true,因此每次单击下一页时,它都会发送下一页页码作为值(第二页为 2),因此偏移量将设置为 2,因此第二页从第三行开始。

    【讨论】:

    • 检查我的问题。我已经删除了这个。你在复制答案
    • 你的答案与我的方法不同,所以我无法理解。找到原始问题的答案后,我立即发布了此答案。
    【解决方案4】:

    这是我的做法:

    class Example extends CI_Controller
    {
        public function index($page = 1){
            // Select the table
            $this->db->from('table_name');
    
            // Total rows
            // NULL for the table name we already specified it
            // FALSE to not clear any filters if you used some before (e.g.: WHERE clause)
            $total_rows = $this->db->count_all_results(NULL, FALSE);
    
            // Load the pagination
            $config['base_url'] = base_url('example');
            $config['total_rows'] = $total_rows;
            $config['per_page'] = 1;
            $config['reuse_query_string'] = TRUE;
            $config['use_page_numbers'] = TRUE;
    
            $this->pagination->initialize($config);
    
            // Prepare the limit to use with the query
            $limit = array(
                    $config['per_page'],
                    ($page - 1) * $config['per_page']
            );
    
            // Set the limit
            $this->db->limit($limit[0], $limit[1]);
    
            // NULL as we already specified the table before
            $query = $this->db->get(NULL);
            // Get the results
            $result = $query->result_array();
    
            // Create pagination links
            $pagination = $this->pagination->create_links();
    
            // Prepare data to pass it to the view page
            $view = array(
                'rows' => $result,
                'pagination' => $pagination
            );
    
            // Pass data to the view
            $this->load->view('page', $view):
        }
    }
    

    我认为你使用的教程是错误的,偏移量不是这样的。

    文档:

    $this-&gt;db-&gt;from()

    $this-&gt;db-&gt;count_all_results()

    $this-&gt;db-&gt;limit()

    $this-&gt;db-&gt;get()

    【讨论】:

    • 我不理解你的一些代码,比如 null 部分。你的意思是我们之前已经指定了表格?您还可以添加如何将数据传递给视图吗?并显示。
    • 这里是$this-&gt;db-&gt;from('table_name');,在声明index()函数后的那一行
    • 你使用查询的方式对我来说是新的,所以我很困惑,我现在正在浏览查询生成器类,看看它们是什么。
    • @ChiefMakii 我添加了使用函数的文档链接。
    • 我可以学习这些,但是如何将数据发送到视图?我像 tihs 一样使用它吗? $data['records'] = $this->db->get(null); $header = array('genre_Id','name'); $this->table->set_heading($header); $this->load->view('pages/sample',$data);
    猜你喜欢
    • 2016-01-30
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多