用户
 找回密码
 入住 CI 中国社区
搜索
查看: 5072|回复: 9
收起左侧

[控制器] 老难了:使用GET方法搜索后的分页问题?

[复制链接]
发表于 2013-7-11 09:04:54 | 显示全部楼层 |阅读模式
本帖最后由 seahorse 于 2013-7-11 09:07 编辑

/*  CI2.1.3  */
用GET方法提交搜索表单,控制器search.php中对搜索的结果进行分页设置,代码如下:
function index(){
         $query = array();
         $category_filter = $this->input->get('category', TRUE);
         if(!empty($category_filter))
         {
                            $query[] = 'category='.$category_filter;
         }
         $direction_filter = $this->input->get(' direction', TRUE);
         if(!empty($direction_filter))
         {
                            $query[] = 'direction='.$ direction_filter;
         }
         // 获取记录总数
         $total_row = $this->My_model->get_research_result_count($category_filter, $direction_filter);
        
         $conifg[‘total_row’]= $total_row;
         $conifg[‘base_url’]= base_url.’index.php/search/index?’.implode(‘$’, $query);
         $conifg[‘per_page’]= ‘8’;
         $conifg[‘uri_segment’]= ‘3’;
         $this->pagination->initialize($config);
         $data[‘searchresult’] = $this->My_model->get_research_result($conifg[‘per_page’], $this->uri->segment(3), $category_filter, $direction_filter);
         $this->load->view(‘main’, $data);
}
模型和视图代码略。
问题:搜索结果第一页能够正常显示,但点击下一页,就显示出数据库的全部记录了。
搜索后地址栏的uri为:
http://localhost/index.php/search/index?category=1&direction=2
点击第2页就显示出数据库所有记录了,地址栏uri为:
http://localhost/index.php/search/index?category=1/8
页数8跑到uri的最后了,而且搜索条件也少了一个。请高手指点?


本帖被以下淘专辑推荐:

发表于 2013-7-11 09:11:04 | 显示全部楼层
 楼主| 发表于 2013-7-12 08:49:13 | 显示全部楼层
To: wingofsky

使用这个方法测试失败。  好像有人把搜索选项存储在session中,但不知道具体如何实现? 请高手指点
发表于 2013-7-12 09:01:23 | 显示全部楼层
seahorse 发表于 2013-7-12 08:49
To: wingofsky

使用这个方法测试失败。  好像有人把搜索选项存储在session中,但不知道具体如何实现? 请 ...

传到SESSION就是直接将所有数据打包传到SESSION里呀
http://codeigniter.org.cn/user_guide/libraries/sessions.html
 楼主| 发表于 2013-7-12 09:10:23 | 显示全部楼层
大哥, 我想知道session解决搜索分页的具体方法,而不是这个帮助手册
发表于 2013-7-17 18:15:03 | 显示全部楼层
本帖最后由 Ahgigu 于 2013-7-17 18:16 编辑

不需要session,
加上suffix配置项就行了
PHP复制代码
$config['suffix'] = strlen($_SERVER['QUERY_STRING']) > 0 ? "?{$_SERVER['QUERY_STRING']}" : '';
$config["first_url"] = $config["base_url"]  . $config["suffix"];//为“第一页”也生成带query string的链接
复制代码


另外在github上最新的实现
https://github.com/EllisLab/CodeIgniter/blob/develop/system/libraries/Pagination.php

增加了一个reuse_query_string配置项,就不需要以上配置了。

希望对你有帮助。
 楼主| 发表于 2013-7-19 08:23:08 | 显示全部楼层
问题解决,非常感谢!
发表于 2013-11-7 16:47:47 | 显示全部楼层
seahorse 发表于 2013-7-19 08:23
问题解决,非常感谢!

请问你有开始用3么?
发表于 2014-4-28 20:27:03 | 显示全部楼层
没有修改CI分页类源码,制作简单的配置即可!
原帖地址:http://codeigniter.org.cn/forums ... hread&tid=18776

// 这段代码和以前的配置没差别
$config['base_url'] =  site_url('shop/membe');

// 追加在所有自动生成的分页链接里的后面,第一页除外
$config['suffix'] = '?mid='.$member_id;

// 第一页默认是$config['base_url'],所有需要手动设置成如下的链接
$config['first_url'] = site_url('shop/membe?mid='.$member_id);
发表于 2015-1-4 00:21:02 | 显示全部楼层
Ahgigu 发表于 2013-7-17 18:15
不需要session,
加上suffix配置项就行了

问题解决,谢谢

本版积分规则