CodeIgniter - 路由和分页

时间:2012-08-14 08:59:05

标签: php codeigniter

这是我在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;
    }

3 个答案:

答案 0 :(得分:0)

以下网址" http://site.com/index.php/user/1/tomek/2"需求: $ route [' User /(:num)/(:any)/(:num)'] =' User / index / $ 1 / $ 2 / $ 3';

您可以将索引留在路线之外。因为这是它所称的标准功能。

答案 1 :(得分:0)

问题实际上在uri_segment。我必须指出,网址的哪一部分是页码。像这样:$config['uri_segment'] = 5;我加载了我的页面:

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

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

答案 2 :(得分:0)

希望这会有所帮助。分页中的两个重要因素是uri段和查询的限制偏移量。如果您尝试完成$ config(不需要用于格式化),例如$ cofig ['uri_segment'],它也可以帮助您。

尝试检查此链接并检查结构和算法,它可能会有所帮助。     Codeigniter, setting up pagination