1

我尝试使用codeigniter的类分页,但我遇到了一个问题,链接的活动页面总是第一页。我的控制器代码

<?php
class Users extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->is_logged_in();
    }

    function index()
    {
        $data['main_content'] = 'admin/users_manage';
        $data['left_menu'] = 'admin/left_menu_users';
        $this->load->view('admin/include/templates_login', $data);
    }

    function select()
    {

        $this->load->library('pagination');
        $this->load->library('table');
        $this->load->model('user_model');

        //$this->table->set_heading('Id', 'The Title', 'The Content');

        $config['base_url'] = 'http://localhost/cib/index.php/admin/users/select';
        $config['total_rows'] = $this->db->get('users_view')->num_rows();
        $config['per_page'] = 10;
        $config['num_links'] = 20;
        //$config['full_tag_open'] = '<div class="pagination">';
        //$config['full_tag_close'] = '</div>';

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

        $data['records'] = $this->user_model->getUsers($config['per_page'], $this->uri->segment(4));
        $data['main_content'] = 'admin/users_manage';
        $this->load->view('admin/include/templates_login', $data);
        //$this->load->view('admin/site_view', $data);
    }

    function is_logged_in()
    {
        $is_logged_in = $this->session->userdata('is_logged_in');
        if(!isset($is_logged_in) || $is_logged_in != true)
        {
            echo 'You don\'t have permission to access this page.';
            echo anchor('admin/login/index', 'Login');
            die();
            //$this->load->view('login_form');
        }
    }
} 
?>

我的模型代码:

<?php
class User_model extends CI_Model
{   
    function validate()
    {
        $username = $this->input->post('username');
        $password = $this->input->post('password');

        $this->db->where('username', $username);
        $this->db->where('password', md5($password));       

        $query = $this->db->get('users');
        if($query->num_rows == 1)
        {
            return true;
        }
        return false;
    }

    function getUsers($num="", $offset="")
    {
        $this->db->limit($num,$offset);
        $query = $this->db->get('users_view');
        return $query;
    }
}

和我的视图页面:admin/include/templates_login.php:

<?php $this->load->view('admin/include/header'); ?>

<?php $this->load->view('admin/include/nav');?>

<?php //if (isset($left_menu)) $this->load->view($left_menu)?>

<?php $this->load->view($main_content); ?>

<?php $this->load->view('admin/include/footer'); ?>

管理员/users_manage.php:

使用 CodeIgniter 进行超级分页

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

Controller中的select()函数是使用CI_Pagination类的函数。我可以获取数据库的不同页面。但是它呈现的链接有一个问题,即第一页链接总是处于活动状态。

在此处输入图像描述

如您所见,它呈现的数据实际上是第 11 页,但活动链接仍然是第一页。(注意,我使用了 bootstrap 和 codeigniter)。

4

1 回答 1

1

您需要将此添加到活动页面链接的代码中:

$config['uri_segment'] = $this->uri->segment(4);
于 2013-07-05T14:13:57.717 回答