0

这是我第一次尝试在 CI 中进行分页,我遇到了一个小问题,可能是由路由引起的。

我的规则如下:

$route['User/(:num)/(:any)'] = 'User/index/$1';
$route['user/(:num)/(:any)'] = 'User/index/$1';

那些被翻译成类似http://site.com/index.php/user/1/tomek.

现在我的分页控制器:

$config['base_url'] = site_url('user').'/'.$user.'/'.$data['user'][0]['nick'].'/';
$config['total_rows'] = $this->rating_model->countOwned($user);
$config['per_page'] = 2;
$config['enable_query_strings'] = true;
$this->pagination->initialize($config);
...
$data['owns'] = $this->rating_model->getOwned($data['user'][0]['id'],$config['total_rows'],$config['per_page']);

在我的模型中,我使用了简单的limit

public function getOwned($user,$limit,$start) {
    $this->db->limit($limit,$start);
    ...

链接和所有工作正常,但它们不起作用。我得到一个类似 的网址http://site.com/index.php/user/1/tomek/2,但物品仍然完好无损。我的错误在哪里?

编辑>完整的控制器代码:

public function index($user = null) {
        $session_data = $this->session->userdata('user_data');
        $this->db->cache_off();

        if($session_data) :

            $this->load->helper(array('form', 'url'));
            $this->load->library('pagination');
            $this->load->model('user_model');
            $this->load->model('rating_model');
            $this->load->model('games_model');

            if($user) {
                $user = $user;
            } else {
                $user = $session_data['id'];
            }

            //vars
                $data['user'] = $this->user_model->getUser($user);
                $data['title'] = $data['user'][0]['nick'].' · Profil użytkownika';
                $data['age'] = $this->user_model->getAge($data['user'][0]['birth']);
                $data['sex'] = $this->user_model->getSex($data['user'][0]['sex']);

                $config['base_url'] = site_url('user').'/'.$user.'/'.$data['user'][0]['nick'].'/';
                $config['total_rows'] = $this->rating_model->countOwned($user);
                $config['per_page'] = 4;
                $config['enable_query_strings'] = true;
                $this->pagination->initialize($config);

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

                $data['rates'] = $this->rating_model->countRates($user);
                $data['rates2'] = $this->rating_model->countGrades($user);

                $data['developers'] = $this->games_model->getDevelopers();
                $data['genres'] = $this->games_model->getGenres();
                $data['platforms'] = $this->games_model->getPlatforms();

                $data['nowPlaying'] = $this->rating_model->getNowPlaying($data['user'][0]['id']);

                $data['owns'] = $this->rating_model->getOwned($data['user'][0]['id'],$config['total_rows'],$config['per_page']);

                //load
                $this->template->load('template','theme/yourAccount/main', $data);

        else :

                $this->load->view('unlogged');

        endif;
    }
4

3 回答 3

0

问题实际上出在uri_segment. 我不得不指出,url的哪一部分是页码。像这样:$config['uri_segment'] = 5;我加载我的页面:

$data['owns'] = $this->rating_model->getOwned($data['user'][0]['id'],4,$this->uri->segment(5));

效果很好。谢谢大家的帮助!

于 2012-08-14T10:47:42.160 回答
0

以下网址“http://site.com/index.php/user/1/tomek/2”需要:$route['User/(:num)/(:any)/(:num)'] = '用户/索引/$1/$2/$3';

您可能可以将 index 排除在路线之外。因为这是它调用的标准函数。

于 2012-08-14T09:13:55.010 回答
0

Hope this will help. 2 important factors in pagination are the uri segment and the limit offset for the query. It may also help you if you will try to complete the $config's(not necessary those used for formatting) like the $cofig['uri_segment'].

Try to check this link and check the structure and algorithm, it might help. Codeigniter, setting up pagination

于 2012-11-28T03:11:41.867 回答