一个关于URL传递参数的问题
在做一个搜索的时候遇到问题,搜索的时候,可以通过关键字和栏目号进行搜索,同时用到$this->load->library('pagination');这个分页类,因为同时涉及到提交搜索的时候POST参数过来和翻页的时候通过..../searchperinfo.php/key/pid/pcount设定控制器内方法,三个参数
function searchPerInfo($key='',$pid=0,$pcount=0){ //key 是关键字,pid是栏目号,pcount是调用分页类传回的下页显示开始数
$key= isset($_POST["key"]) && !empty($_POST["key"]) ? $this->cleanInfo($_POST["key"]) : '$key;
$pid= isset($_POST["pid"]) && !empty($_POST["pid"]) ? $this->cleanInfo($_POST["pid"]) : $pid;
$page = isset($_POST["p_count"]) && !empty($_POST["p_count"]) ? $_POST["p_count"] : $p_count;
$this->load->library('pagination');//分页类
$config['base_url'] = "http://localhost/sportsdata/index.php/searchinfo/searchPerInfo/".$key."/".$pid;设置分页类的URL
..................
}
这时就产生一个问题,因为$key是可能为空的,当$key为空的时候url在点击下一页翻页的时候就少了参数,变成。。。/searchPerInfo/pid/p_count了,因为key没有值,就算是不为空的时候翻页的结果好像也不对,这个传递参数的问题一直困扰我,谁能帮我写个小例子,就是搜索的时候除了post过来参数,还有在翻页的时候同时要带参数。
是我对CI的参数传递理解的不对?谁能帮帮忙!! 看看这样行不行:http://codeigniter.org.cn/forums/thread-692-1-1.html 到现在我也想不出好办法,谁有妙方拿出来分享一下下~~
我以前是用土方法弄的
$kw = $this->uri->segment(3);
$config['base_url'] = ($kw=='CustomNullValue'||$kw=='')?'/index.php/a/b/CustomNullValue':'/index.php/a/b/'.$kw;
//判断关键字CustomNullValue和为空值时的sql语句(略)
[ 本帖最后由 pp18180058 于 2008-7-28 23:38 编辑 ] 用了hex的方法, 在config.php 中,将‘uri_protocol’ 设置为 ‘PATH_INFO’.
$config['uri_protocol'] = "PATH_INFO";
然后修改分页类,Pagination.php
120行,注销掉
/*
$CI =& get_instance();
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;
}
*/
然后修改160行
//$this->base_url = rtrim($this->base_url, '/') .'/';
$this->base_url = rtrim($this->base_url, '/'); //去掉分页类自己增加的“/”
然后在控制器里面将cur_page参数传入
parse_str($_SERVER['QUERY_STRING'],$_GET);
$key_g = isset($_GET["key"]) && !empty($_GET["key"]) ? $this->cleanInfo($_GET["key"]) : '';
$pid_g= isset($_GET["pid"]) && !empty($_GET["pid"]) ? $this->cleanInfo($_GET["pid"]) : 0;
$page_g = isset($_GET["p_count"]) && !empty($_GET["p_count"]) ? $_GET["p_count"] : 0;
$key= isset($_POST["key"]) && !empty($_POST["key"]) ? $this->cleanInfo($_POST["key"]) : $key_g;
$pid= isset($_POST["pid"]) && !empty($_POST["pid"]) ? $this->cleanInfo($_POST["pid"]) : $pid_g;
$page = isset($_POST["p_count"]) && !empty($_POST["p_count"]) ? $_POST["p_count"] : $page_g;
$config['base_url'] = site_url("/searchinfo/searchPerInfo/?key=".$key."&pid=".$bid."&p_count=");设置分页链接为GET方式
$config['cur_page'] = $page; //不让分页类自己去取,直接传进去
是能把问题解决了,不过总是觉得有些别扭 对了,改成GET还有个好处,传递参数是中文的时候不用转码,很多搜索中文关键字的时候比较方便 CI 1.6.3 的分页类支持 Query String,楼主看一下手册。
页:
[1]