如何创建分页的url参数在codeigniter? [英] how to create pagination for url with parameters in codeigniter?

查看:134
本文介绍了如何创建分页的url参数在codeigniter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个基于某些参数(例如 domain.com/index.php/welcome/student/male )的页面结果的应用程序 welcome 是控制器学生是方法名男性是参数,现在我们要分页这个,但问题是一个人也可以使用子类别像这样 domain.com/index.php/welcome/student/male/steven 其中 steven 是另一个参数,如何为此类型的网址创建分页。提前感谢:)

we have an application which has page's result on the bases of certain parameters like domain.com/index.php/welcome/student/male here welcome is controller student is method name male is parameter, now we want pagination for this, but the problem is a person can also use sub category like this domain.com/index.php/welcome/student/male/steven where steven is another parameter, how to create pagination for this type of urls. Thank you in advance :)

推荐答案

有可选参数和分页时的一些问题:

Some problems when having optional parameters along with pagination:


  1. 根据提供的参数的
    编号,在网址中放置分页偏移变化。

  2. 如果可选参数不是

方法1: >
使用查询字符串进行分页:

Method 1:
Use query strings for pagination:

function student($gender, $category = NULL) {
    $this->load->library('pagination');
    $config['page_query_string'] = TRUE;
    $config['query_string_segment'] = 'offset';

    $config['base_url'] = base_url().'test/student/'.$gender;
    // add the category if it's set
    if (!is_null($category)) 
        $config['base_url'] = $config['base_url'].'/'.$category;

    // make segment based URL ready to add query strings
    // pagination library does not care if a ? is available
    $config['base_url'] = $config['base_url'].'/?';


    $config['total_rows'] = 200;
    $config['per_page'] = 20;
    $this->pagination->initialize($config);

    // requested page:
    $offset = $this->input->get('offset');

    //...
}

方法2:

假设类别永远不是一个数字,如果最后一个段是一个数值,那么它的分页偏移量函数的参数:

Method 2:
Assuming the category would never be a number and if the last segment is a numeric value then it's the pagination offset not a function's parameter:

function student($gender, $category = NULL) {
    // if the 4th segment is a number assume it as pagination rather than a category
    if (is_numeric($this->uri->segment(4))) 
        $category = NULL;

    $this->load->library('pagination');
    $config['base_url'] = base_url().'test/student/'.$gender;
    $config['uri_segment'] = 4;

    // add the category if it's set
    if (!is_null($category)) {
        $config['uri_segment'] = $config['uri_segment'] + 1;
        $config['base_url'] = $config['base_url'].'/'.$category;
    }

    $config['total_rows'] = 200;
    $config['per_page'] = 20;
    $this->pagination->initialize($config);

    // requested page:
    $offset = ($this->uri->segment($config['uri_segment'])) ? $this->uri->segment($config['uri_segment']) : 1;

    //...
}   

第一种方法,因为它不干扰函数参数,并且将更容易在抽象层面实现分页支持。

I prefer the first method because it does not interfere with function parameters and it would be easier to implement the pagination support at an abstract level.

这篇关于如何创建分页的url参数在codeigniter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆