【问题标题】:Codeigniter Pagination Show Number of Results & PageCodeigniter 分页显示结果数和页数
【发布时间】:2017-10-17 05:28:32
【问题描述】:

我正在使用 Codeigniter 3 并且有一个简单的 PHP 应用程序。

使用分页类,我想在每个页面的顶部显示以下内容;

显示 z 个结果中的 x 到 y 个结果

在哪里;

x = start row
y - end row
z = total rows

`Showing 1 to 10 of 5213 results.`
`Showing 11 to 20 of 5213 results.`
`etc`

我可以使用$config['total_rows'] 变量检索总行数。不过其他的就不清楚了。

我的 items.php 控制器看起来像这样;

    public function index() {
        $config['base_url'] = '/items/index';
        $config['use_page_numbers'] = FALSE; 
        $config['reuse_query_string'] = TRUE;
        $config['total_rows'] = $this->db->get('item')->num_rows();
        $config['per_page'] = 10;
        $config['num_links'] = 10;
        $config['full_tag_open'] = '<div><ul class="pagination">';
        $config['full_tag_close'] = '</ul></div><!--pagination-->';
        $config['first_link'] = '&laquo; First';
        $config['first_tag_open'] = '<li class="prev page">';
        $config['first_tag_close'] = '</li>';
        $config['last_link'] = 'Last &raquo;';
        $config['last_tag_open'] = '<li class="next page">';
        $config['last_tag_close'] = '</li>';
        $config['next_link'] = 'Next &rarr;';
        $config['next_tag_open'] = '<li class="next page">';
        $config['next_tag_close'] = '</li>';
        $config['prev_link'] = '&larr; Previous';
        $config['prev_tag_open'] = '<li class="prev page">';
        $config['prev_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active"><a href="">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li class="page">';
        $config['num_tag_close'] = '</li>';
        $config['anchor_class'] = 'follow_link';
        $this->load->library('pagination');
        $this->pagination->initialize($config);

        $data = array(
        'items' => $this->items_model->itemList()
        );

        $this->load->view('item_list', $data);
    }

我的网址采用以下格式;

items/index    // displays results 1-10
items/index/10 // displays results 11-20
items/index/20 // displays results 21-30

任何帮助将不胜感激。 谢谢

【问题讨论】:

  • 我做了一个答案,但没有注意 url 格式,我把它当作 1、2、3 等等。
  • 当您在items/index/10 页面上时,您是否获得了 1-10 个结果?
  • 设置,立即查看。

标签: php codeigniter pagination codeigniter-3


【解决方案1】:

我很惊讶没有提到扩展分页库?更加优雅,并且可以根据需要添加其他功能。

application/libraries/MY_Pagination.php

<?php

defined('BASEPATH') or exit('No direct script access allowed');

class MY_Pagination extends CI_Pagination
{
    /**
     * Total number of items
     *
     * @var int
     */
    protected $total_rows = 0;

    public function __construct($params = array())
    {
        parent::__construct($params);
    }

    public function get_total_rows(): int
    {
        return $this->total_rows;
    }
}

【讨论】:

    【解决方案2】:
    $data['z'] = $config['total_rows'];
    
    $data['x'] = (int)$this->uri->segment(3) + 1;
    
    if ($this->uri->segment(3) + $config['per_page'] > $config['total_rows']) {
        $data['y'] = $config['total_rows'];
    } else {
        $data['y'] = (int)$this->uri->segment(3) + $config['per_page'];
    }
    

    此外,如果请求的页面在范围内,您可以在加载视图之前进行一次检查,如果不在范围内,您应该根据您的要求将访问者重定向到第一页或最后一页。

    【讨论】:

    • 这段代码应该进入控制器。它应该以这种方式工作。应放置在视图负载线之前。通过传递的$data 数组,所有值 x、y、z 将在视图中可用。你已经做到了。或者我不确定问的是什么:你能澄清一下吗?
    • 也许这取决于分离的视图文件。即如果您有不同的部分文件但也想在其中包含变量。我只是在猜测这个。为了避免这种情况,还有其他方法可以将数据注入视图。 $this-&gt;load-&gt;vars($data);$this-&gt;load-&gt;view('item_list');。这样,从控制器传递的数据将可用于单个请求中包含的所有视图文件。
    • 另外,你必须把我提供的sn-p放在$data = array('items' =&gt; $this-&gt;items_model-&gt;itemList());之后。否则你将覆盖数组。
    • 原来是这样 - 我在 之前 $data = array('items' =&gt; $this-&gt;items_model-&gt;itemList()); 放置了代码。我不知道这会有所作为?!非常感谢@Tpojka
    • 检查here。 #SOreadyToHelp 快乐编码。 ;)
    【解决方案3】:

    将以下代码放在分页功能下方的控制器中:-

    $start= (int)$this->uri->segment(3) * $config['per_page']+1;
    $end = ($this->uri->segment(3) == floor($config['total_rows']/ $config['per_page']))? $config['total_rows'] : (int)$this->uri->segment(3) * $config['per_page'] + $config['per_page'];
    
    $data['result_count']= "Showing ".$start." - ".$end." of ".$config['total_rows']." Results";
    

    在你看来:-

    <?php echo $result_count;?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-26
      • 2014-03-26
      相关资源
      最近更新 更多