1

我正在建立一个网站,上面有很多分页页面,上面有成百上千的项目。我想显示当前页面上显示的项目范围,如下所示:

| 115-130 出 300 |

我已经尝试过了,但它不能正常工作

echo "Showing ".( $page == 1 ? 1 : ($page -1) * $config["per_page"] +1 )." to ".($page *  $config["per_page"])." item of ".$config["total_rows"];

我控制器中的当前代码是:

class Home extends CI_Controller {

function __construct()
{
    parent::__construct();
    $this->load->library("pagination");
}

public function index()
{
    $this->load->model('status_model');//load the status model

    //pagination config
    $config = array();
    $config["base_url"] = site_url() . "/home/index";
    $config["total_rows"] = $this->status_model->count_active_entries();//Get the total number of rows
    $config["per_page"] = 1;
    $config["uri_segment"] = 3;
    $config["anchor_class"] = 'class="normal" ';
    $config['full_tag_open'] = '<ul>';
    $config['full_tag_close'] = '</ul>';
    $config['first_tag_open'] = '<li>';
    $config['first_tag_close'] = '</li>';
    $config['next_link'] = 'Next';
    $config['next_tag_open'] = '<li>';
    $config['next_tag_close'] = '</li>';
    $config['last_tag_open'] = '<li>';
    $config['last_tag_close'] = '</li>';
    $config['prev_link'] = 'Prev';
    $config['prev_tag_open'] = '<li>';
    $config['prev_tag_close'] = '</li>';
    $config['cur_tag_open'] = '<li class="num_active">';
    $config['cur_tag_close'] = '</li>';
    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';


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

    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

    $statuses = $this->status_model->get_entries($config["per_page"], $page); //call the method to get all the active statuses

    if($statuses !== false) { 

        $content_data['results'] = $statuses;
    }
    else { // If no results are returned display the following error message

        $content_data['error'] = 'Error no status updates are being returned from the database. Please contact an administrator.';
    }

    echo "Showing ".( $page == 1 ? 1 : ($page -1) * $config["per_page"] +1 )." to ".($page * $config["per_page"])." item of ".$config["total_rows"];


    $content_data["links"] = $this->pagination->create_links();
    $data['title'] = ' Status';                           //Browser page title
    $data['page_title'] = 'The Latest Status Updates';//The page H1 tag
    $data['content'] = $this->load->view('results', $content_data, TRUE);//the page content

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

}
4

1 回答 1

0

首先在删除以下行后尝试

echo "Showing ".( $page == 1 ? 1 : ($page -1) * $config["per_page"] +1 )." to ".($page * $config["per_page"])." item of ".$config["total_rows"];

并检查您的页面。如果您的页面没有错误地呈现并且分页工作正常,那么您应该通过计算获取的行数来计算范围并将计算的范围传递给查看。

 $count = count($statuses);
 $r_from = $page <= 1 ? 1 : ($page -1) * $config["per_page"]+1; //RANGE STARTS FROM 
 $r_to   = $r_from + $count - 1;    //RANGE ENDS TO
 $content_data["range"] = $count? "Showing ".$r_from." to ".$r_to." item of ".$config["total_rows"]:'';

在您打印 $content_data["links"]; 时在您的视图上打印它 可能是这样的

echo $range;

完整的控制器代码-

class Home extends CI_Controller {

function __construct()
{
    parent::__construct();
    $this->load->library("pagination");
}

public function index()
{
    $this->load->model('status_model');//load the status model

    //pagination config
    $config = array();
    $config["base_url"] = site_url() . "/home/index";
    $config["total_rows"] = $this->status_model->count_active_entries();//Get the total number of rows
    $config["per_page"] = 1;
    $config["uri_segment"] = 3;
    $config["anchor_class"] = 'class="normal" ';
    $config['full_tag_open'] = '<ul>';
    $config['full_tag_close'] = '</ul>';
    $config['first_tag_open'] = '<li>';
    $config['first_tag_close'] = '</li>';
    $config['next_link'] = 'Next';
    $config['next_tag_open'] = '<li>';
    $config['next_tag_close'] = '</li>';
    $config['last_tag_open'] = '<li>';
    $config['last_tag_close'] = '</li>';
    $config['prev_link'] = 'Prev';
    $config['prev_tag_open'] = '<li>';
    $config['prev_tag_close'] = '</li>';
    $config['cur_tag_open'] = '<li class="num_active">';
    $config['cur_tag_close'] = '</li>';
    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';


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

    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

    $statuses = $this->status_model->get_entries($config["per_page"], $page); //call the method to get all the active statuses
    $count = '';
    if($statuses !== false) { 

        $content_data['results'] = $statuses;
        $count = count($statuses);
        $r_from = $page <= 1 ? 1 : ($page -1) * $config["per_page"]+1;
        $r_to   = $r_from + $count - 1;
        // YOU CAN DO IT WITHOUT COUNT. FOR THAT YOU NEED TO CALCULATE $r_to 
        // $r_to = $page * $config["per_page"];
        // AND NEED TO CHECK THIS WITH Total record
        // $r_to = $r_to>$config["total_rows"] ? $config["total_rows"]:$r_to;
    }
    else { // If no results are returned display the following error message

        $content_data['error'] = 'Error no status updates are being returned from the database. Please contact an administrator.';
    }


    $content_data["range"] = $count? "Showing ".$r_from." to ".$r_to." item of ".$config["total_rows"]:'';

    $content_data["links"] = $this->pagination->create_links();
    $data['title'] = ' Status';                           //Browser page title
    $data['page_title'] = 'The Latest Status Updates';//The page H1 tag
    $data['content'] = $this->load->view('results', $content_data, TRUE);//the page content

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

}
于 2013-07-24T05:21:27.173 回答