【问题标题】:codeigniter: pagination + table libraries (not working)codeigniter:分页+表格库(不工作)
【发布时间】:2011-07-21 02:34:47
【问题描述】:

我的代码点火器应用程序需要帮助..我似乎无法找出导致分页不起作用的原因

这是我的控制器

$limit = 15;
        $uri_segment = 4;
        $this->load->helper('string');

        $offset = $this->uri->segment( $uri_segment );
        log_message('error',date('Y-m-d H:i:s', time()));
        //load
        //$products = $this->Products_model->get_all();
        $products = $this->Products_model->get_products($limit, $uri_segment);
        //var_dump($products);

        log_message('error', $products );

        //pagination
        $this->load->library('pagination');
        $config['base_url'] = site_url('admin/products/index/');
        $config['total_rows'] = $this->Products_model->count_all();
        $config['per_page'] = $limit;
        $config['uri_segment'] = $uri_segment;
        $this->pagination->initialize($config);

        $data['pagination'] = $this->pagination->create_links();

        //table
        $this->load->library('table');
        $this->table->set_empty(" ");
        $this->table->set_heading('ID', 'Product','Category', 'Actions');
        $i = 0 + $offset;
        foreach ($products as $product){
            ++$i;
            $this->table->add_row($product->id, $product->title, $this->Categories_model->get_by_id($product->category_id)->title,
                anchor('/admin/products/update/'.$product->id,'update',array('class'=>'update')).' '.
                anchor('/admin/products/delete/'.$product->id,'delete',array('class'=>'delete','onclick'=>"return confirm('Are you sure want to delete this product?')"))
            );
        }
        $data['table'] = $this->table->generate();

        //load template
        $this->products_list();
        $this->products_template();
        $this->template->build('products/productList', $data);

这是我的模型

function get_products($num, $offset) {
    return $this->db->get($this->tbl_products, $num, $offset)->result();    
}

它会生成链接,但不会加载下一组记录。对上面的代码有任何更正吗?

谢谢!

【问题讨论】:

  • 我很确定分页库只是为您生成链接,您可以将偏移量和限制传递给模型的 get() 函数(或任何您调用的函数)。跨度>
  • 没错,看看get_where,它有你需要的东西。
  • 你能给我一个提示或样本吗?我刚接触 php 编程

标签: php codeigniter pagination


【解决方案1】:

您应该将页面提供给表格,而不是整个表格。分页只是填充链接,但不会对您的数据进行任何更改,这是您的责任。

请更改:

        $products = $this->Products_model->get_all();

与:

        $products = $this->Products_model->get_page( $Offset, $this->limit ); // It's not clear where do you define limit.

并在您的模型中进行适当的选择:

function get_page( $offset, $limit ){
    return $this->db->get( $this->tbl_products, $limit, $offset )->result();
}

【讨论】:

  • 嗨,我已经按照你的建议更新了我的代码..但是它不会加载下一组记录
  • 请检查分页链接中'offset'的值。你能给我记录的数量和“限制”值吗?您的代码中不清楚
  • 你写了'$products = $this->Products_model->get_products($limit, $uri_segment);',把'$uri_segment'改成'$offset'
【解决方案2】:

您应该确保您的 limit($this->limit) 具有良好的数据(仅限整数)。如果您打开了查询字符串,您可能会将字符串 '&per_page=' 传递给您的模型。如果使用正则表达式或 str_replace 来清除 $this->limit。

【讨论】:

    【解决方案3】:

    这是你的错误

    $products = $this->Products_model->get_products($limit, $uri_segment);
    
    $limit=the number of data you want to show in per page,
    
    $offset=the data number start from.
    

    因此,单击每个链接,您必须更改偏移值以显示数据。试试这个

    if($this->uri->segment(uri_segment) > 0){
    
                    $offset=$this->uri->segment($uri_segment);
    
                }
                else{
                    $offset=0;
    
                }
    $products = $this->Products_model->get_products($limit, $offset);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-27
      • 1970-01-01
      • 2014-01-13
      • 1970-01-01
      相关资源
      最近更新 更多