我正在使用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'] = '« First';
$config['first_tag_open'] = '<li class="prev page">';
$config['first_tag_close'] = '</li>';
$config['last_link'] = 'Last »';
$config['last_tag_open'] = '<li class="next page">';
$config['last_tag_close'] = '</li>';
$config['next_link'] = 'Next →';
$config['next_tag_open'] = '<li class="next page">';
$config['next_tag_close'] = '</li>';
$config['prev_link'] = '← 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
任何帮助将不胜感激。 感谢
答案 0 :(得分: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'];
}
此外,如果请求的页面在范围内,您可以在加载视图之前进行一次检查,如果不是,则应根据您的要求将访问者重定向到第一页或最后一页。
答案 1 :(得分:1)
将以下代码放在控制器中的分页功能下方: -
$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;?>
答案 2 :(得分:0)
我很惊讶没有提到扩展分页库吗?更加优雅,可以根据需要添加其他功能。
应用程序/库/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;
}
}