收藏本站 Archiver
    请 后使用快捷导航
    没有账号?入住 CI 中国社区
    查看: 8174|回复: 10

    URL带有参数,CI的分页如何制作

    [复制链接]
    楼主
    发表于 2012年7月18日 11:43:17 | 只看该作者 回帖奖励 |倒序浏览 |
    本帖最后由 x171306523c 于 2012年7月18日 11:44 编辑

    在一个from表单里,method使用get,点击提交后,URL会跟随参数
    如,在from表单里填写Wave Pad Audio Editor mp3,点提交,则URL为
    http://localhost/index.php/s/wd/?wd=Wave+Pad+Audio+Editor+mp3

    通过搜索,得到数据库里全部关于关键词的内容,然后通过分页,分页显示内容
    但是这个分页我得怎么做?这个URL是带有参数的,如果把从?到最后的参数去掉那就不是我想要的内容了

    评分

    参与人数 1 威望 +5 理由
    Aloghli + 5 很给力!

    查看全部评分

    沙发
    发表于 2012年7月18日 12:55:42 | 只看该作者
    做一个MY_Pagination.php
    页面有$this->query_string = '';
    初始化的时候给query_string赋值你的GET
    然后在MY_Pagination.php里的第个连接后面加个$this->query_string
    如果你不想用GET的时候在初始化时不用赋值不就OK了吗
    CI没有写这个,但是咱们可以想啊
    藤椅
    楼主| 发表于 2012年7月18日 16:49:30 | 只看该作者
    本帖最后由 x171306523c 于 2012年7月18日 16:52 编辑
    sdink 发表于 2012年7月18日 12:55
    做一个MY_Pagination.php
    页面有$this->query_string = '';
    初始化的时候给query_string赋值你的GET


    不对啊
    初始化的时候给query_string赋值你的GET
    第一页:query_string的值为Wave Pad Audio Editor mp3
    第二页:query_string的值为Wave Pad Audio Editor mp3/2
    第三页:query_string的值为Wave Pad Audio Editor mp3/3
    以此类推

    还是我没明白您的意思啊?
    请赐教!!!


    板凳
    发表于 2012年7月18日 17:55:42 | 只看该作者
    要不要我上传一个给你啊。。。
    报纸
    发表于 2012年7月18日 17:57:27 | 只看该作者
    PHP复制代码


    <?php
    if (! defined ( 'BASEPATH' ))
    exit ( 'No direct script access allowed' );

    class MY_Pagination extends CI_Pagination {

    var $base_url = '';
    var $prefix = ''; // A custom prefix added to the path.
    var $suffix = ''; // A custom suffix added to the path.


    var $total_rows = 0;
    var $per_page = 10;
    var $num_links = 5;
    var $cur_page = 0;
    var $use_page_numbers = FALSE;
    var $first_link = '&lsaquo; First';
    var $next_link = '>';
    var $prev_link = '<';
    var $last_link = 'Last &rsaquo;';
    var $uri_segment = 4;
    var $full_tag_open = '';
    var $full_tag_close = '';
    var $first_tag_open = '';
    var $first_tag_close = '&nbsp;';
    var $last_tag_open = '&nbsp;';
    var $last_tag_close = '';
    var $first_url = ''; // Alternative URL for the First Page.
    var $cur_tag_open = '&nbsp;<strong>';
    var $cur_tag_close = '</strong>';
    var $next_tag_open = '&nbsp;';
    var $next_tag_close = '&nbsp;';
    var $prev_tag_open = '&nbsp;';
    var $prev_tag_close = '';
    var $num_tag_open = '&nbsp;';
    var $num_tag_close = '';
    var $page_query_string = FALSE;
    var $query_string_segment = 'per_page';
    var $display_pages = TRUE;
    var $anchor_class = '';

    /**
    * Constructor
    *
    * @access public
    * @param array initialization parameters
    */

    public function __construct($params = array ()) {
    $this->full_tag_open = '<div id="page">';
    $this->full_tag_close = '</div>';
    $this->first_link = '首页';
    $this->first_tag_open = ' ';
    $this->first_tag_close = ' ';
    $this->last_link = '首页';
    $this->last_tag_open = ' ';
    $this->last_tag_close = ' ';
    $this->cur_tag_open = '<b>';
    $this->cur_tag_close = '</b> ';
    $this->query_string = '';
    if (count ( $params ) > 0) {
    $this->initialize ( $params );
    }

    if ($this->anchor_class != '') {
    $this->anchor_class = 'class="' . $this->anchor_class . '" ';
    }

    }

    // --------------------------------------------------------------------


    /**
    * Initialize Preferences
    *
    * @access public
    * @param array initialization parameters
    * @return void
    */

    function initialize($params = array ()) {
    if (count ( $params ) > 0) {
    foreach ( $params as $key => $val ) {
    if (isset ( $this->$key )) {
    $this->$key = $val;
    }
    }
    }
    }

    function create_links() {
    // If our item count or per-page total is zero there is no need to continue.
    if ($this->total_rows == 0 or $this->per_page == 0) {
    return '';
    }

    // Calculate the total number of pages
    $num_pages = ceil ( $this->total_rows / $this->per_page );

    // Is there only one page? Hm... nothing more to do here then.
    if ($num_pages == 1) {
    return '';
    }

    // Determine the current page number.
    $CI = & get_instance ();

    if ($CI->config->item ( 'enable_query_strings' ) === TRUE or $this->page_query_string === TRUE) {
    if ($CI->input->get ( $this->query_string_segment ) != 0) {
    $this->cur_page = $CI->input->get ( $this->query_string_segment );

    // Prep the current page - no funny business!
    $this->cur_page = ( int ) $this->cur_page;
    }
    } else {
    if ($CI->uri->segment ( $this->uri_segment ) != 0) {
    $this->cur_page = $CI->uri->segment ( $this->uri_segment );

    // Prep the current page - no funny business!
    $this->cur_page = ( int ) $this->cur_page;
    }
    }

    $this->num_links = ( int ) $this->num_links;

    if ($this->num_links < 1) {
    show_error ( 'Your number of links must be a positive number.' );
    }

    if (! is_numeric ( $this->cur_page )) {
    $this->cur_page = 0;
    }

    if ($this->cur_page > $this->total_rows) {
    $this->cur_page = ($num_pages - 1) * $this->per_page;
    }

    $uri_page_number = $this->cur_page;
    $this->cur_page = floor ( ($this->cur_page / $this->per_page) + 1 );

    $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
    $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;

    if ($CI->config->item ( 'enable_query_strings' ) === TRUE or $this->page_query_string === TRUE) {
    $this->base_url = rtrim ( $this->base_url ) . '&amp;' . $this->query_string_segment . '=';
    } else {
    $this->base_url = rtrim ( $this->base_url, '/' ) . '/';
    }

    $output = '';

    if ($this->first_link !== FALSE and $this->cur_page > ($this->num_links + 1)) {
    $first_url = ($this->first_url == '') ? $this->base_url : $this->first_url;
    $output .= $this->first_tag_open . '<a ' . $this->anchor_class . 'href="' . site_url ( $first_url ) . $this->query_string . '">' . $this->first_link . '</a>' . $this->first_tag_close;
    }

    if ($this->prev_link !== FALSE and $this->cur_page != 1) {
    $i = $uri_page_number - $this->per_page;

    if ($i == 0 && $this->first_url != '') {
    $output .= $this->prev_tag_open . '<a ' . $this->anchor_class . 'href="' . site_url ( $CI->config->item ( 'system' ).$this->first_url ) . $this->query_string . '">' . $this->prev_link . '</a>' . $this->prev_tag_close;
    } else {
    $i = ($i == 0) ? '' : $this->prefix . $i . $this->suffix;
    $output .= $this->prev_tag_open . '<a ' . $this->anchor_class . 'href="' . site_url ( $CI->config->item ( 'system' ).$this->base_url . $i ) . $this->query_string . '">' . $this->prev_link . '</a>' . $this->prev_tag_close;
    }

    }

    if ($this->display_pages !== FALSE) {
    for($loop = $start - 1; $loop <= $end; $loop ++) {
    $i = ($loop * $this->per_page) - $this->per_page;

    if ($i >= 0) {
    if ($this->cur_page == $loop) {
    $output .= $this->cur_tag_open . $loop . $this->cur_tag_close; // Current page
    } else {
    $n = ($i == 0) ? '' : $i;

    if ($n == '' && $this->first_url != '') {
    $output .= $this->num_tag_open . '<a ' . $this->anchor_class . 'href="' . site_url ($CI->config->item ( 'system' ). $this->first_url ) . $this->query_string . '">' . $loop . '</a>' . $this->num_tag_close;
    } else {
    $n = ($n == '') ? '' : $this->prefix . $n . $this->suffix;

    $output .= $this->num_tag_open . '<a ' . $this->anchor_class . 'href="' . site_url ( $CI->config->item ( 'system' ).$this->base_url . $n ) . $this->query_string . '">' . $loop . '</a>' . $this->num_tag_close;
    }
    }
    }
    }
    }

    if ($this->next_link !== FALSE and $this->cur_page < $num_pages) {
    $output .= $this->next_tag_open . '<a ' . $this->anchor_class . 'href="' . site_url ( $CI->config->item ( 'system' ).$this->base_url . $this->prefix . ($this->cur_page * $this->per_page) ) . $this->suffix . $this->query_string . '">' . $this->next_link . '</a>' . $this->next_tag_close;
    }

    if ($this->last_link !== FALSE and ($this->cur_page + $this->num_links) < $num_pages) {
    $i = (($num_pages * $this->per_page) - $this->per_page);
    $output .= $this->last_tag_open . '<a ' . $this->anchor_class . 'href="' . site_url ( $CI->config->item ( 'system' ).$this->base_url . $this->prefix . $i ) . $this->suffix . $this->query_string . '">' . $this->last_link . '</a>' . $this->last_tag_close;
    }

    $output = preg_replace ( "#([^:])//+#", "\\1/", $output );
    $output = $this->full_tag_open . $output . $this->full_tag_close;

    return $output;
    }
    }

    复制代码
    地板
    发表于 2012年7月18日 18:00:54 | 只看该作者
    看吧

    MY_Pagination.rar

    2012年7月18日 18:00 上传

    点击文件名下载附件

    1.96 KB, 下载次数: 77

    7
    楼主| 发表于 2012年7月23日 17:39:54 | 只看该作者
    sdink 发表于 2012年7月18日 18:00
    看吧

    非常非常感谢 sdink
    8
    发表于 2012年7月27日 16:59:23 | 只看该作者
    设置你的base_url,$condition为你的查询条件,表单提交GET方式
    PHP复制代码
    $config['base_url'] = site_url() . '/controller/function/?condition1=' . $condition1;
    复制代码


    然后开启page_query_string
    PHP复制代码
    $config['page_query_string'] = TRUE;
    复制代码


    最后出现的URL是这样的:
    PHP复制代码
    www.xxx.com/contorller/function/?condition=admin&per_page=XX
    复制代码

    &per_page=XX是开启page_query_string后,自动在url后追加的。
    9
    发表于 2013年6月20日 11:30:01 | 只看该作者
    x171306523c 发表于 2012年7月18日 16:49
    不对啊
    初始化的时候给query_string赋值你的GET
    第一页:query_string的值为Wave Pad Audio Editor mp3

    我也遇到同样的问题
    10
    发表于 2013年8月20日 10:52:32 | 只看该作者
    Nebula 发表于 2012年7月27日 16:59
    设置你的base_url,$condition为你的查询条件,表单提交GET方式

    我也是像你那么设置的,可是页数是对了,但是在点击下一页或者上一页的时候,查询的数据不变啊。
    下一页 »
    12 下一页
    返回列表
    您需要登录后才可以回帖 登录 | 入住 CI 中国社区

    本版积分规则

    AltStyle によって変換されたページ (->オリジナル) /