MVC结构中的分页

时间:2013-12-19 11:32:52

标签: php regex .htaccess mod-rewrite pagination

任何人都可以帮我解决如何在mvc结构中调用分页函数的问题吗?

.htaccess完成其魔法之后,我看起来像是这样的链接:

controller/action/id

在:

index.php?controller=&action=&id=

我真正想做的是设置网址

/controller/px

x是任何int值

我的.htaccess

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&id=$3 [NC,L,QSA]

我希望得到一些帮助,或者任何类型的指针都会很好。

2 个答案:

答案 0 :(得分:1)

您可以在现有规则之前添加此规则

RewriteRule ^([a-zA-Z]+)/([0-9]+)/?$ index.php?controller=$1&action=&id=&p=$2 [L,QSA]

答案 1 :(得分:0)

for htaceess file:

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /your_folder_name/
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php?/$1 [L]
    </IfModule>

    <IfModule !mod_rewrite.c>
        # If we don't have mod_rewrite installed, all 404's
        # can be sent to index.php, and everything works as normal.
        # Submitted by: ElliotHaughin

        ErrorDocument 404 /index.php
    </IfModule>

Replace your_folder_name on the third line with your folder name. 

if its code ignitor, you need to make some changes in application/config/config.php 
change the following lines as below

$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

also in autoload in application/config/autoload.php, change to the following line

$autoload['helper'] = array('url');

In Controller

$this->load->library('Pagination');
$config['base_url']=base_url().'controller/function';
$config['total_rows']=$data_count;
$config['per_page']=1;
$config['num_links']=2;
$this->pagination->initialize($config);

While fetching data from database use uri segment  and $config['per_page'] as limits

In View

to get the pagination links 

echo $this->pagination->create_links();