在uri - Codeigniter中获取GET数据 [英] Pagnation with GET data in the uri - Codeigniter

查看:116
本文介绍了在uri - Codeigniter中获取GET数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图分页DB返回的结果。但是当我尝试从URI获取偏移量时:



questions / search?content = foobar / 4 p>

/ 4 应为偏移量,但分配给 $ _ GET value。



这是控制器中的整个方法:



http://pastebin.com/QFJddMDJ

  $ results = $ this-> question-> search_results_count($ content); 
$ this-> load-> library('pagination');
$ config ['total_rows'] = count($ results);
$ offset = $ this-> uri-> segment(3);
if($ offset == false)$ offset = 0;
$ config ['full_tag_open'] ='< ul class =pages>';
$ config ['full_tag_close'] ='< / ul>';
$ config ['num_tag_open'] ='< li>';
$ config ['num_tag_close'] ='< / li>';
$ config ['cur_tag_open'] ='< li>< a class =active>';
$ config ['cur_tag_close'] ='< / a>< / li>';
$ config ['prev_tag_open'] ='< li class =prev>';
$ config ['prev_tag_close'] ='< li>';
$ config ['next_tag_open'] ='< li class =next>';
$ config ['next_tag_close'] ='< / li>';
$ config ['num_tag_open'] ='< li>';
$ config ['num_tag_close'] ='< / li>';
$ config ['first_link'] ='<<';
$ config ['first_tag_open'] ='< li>';
$ config ['first_tag_close'] ='< / li>';
$ config ['last_link'] ='>>';
$ config ['last_tag_open'] ='< li>';
$ config ['last_tag_close'] ='< / li>';
$ config ['per_page'] = 1;
$ config ['uri_segment'] = 2;
$ config ['page_query_string'] = TRUE;
$ config ['use_page_numbers'] = TRUE;
$ config ['suffix'] ='?content ='。$ content;
$ config ['base_url'] = base_url()。'questions / search /';
$ this-> pagination-> initialize($ config);


解决方案

t工作这样。查询字符串必须在结尾(或在散列片段之前)。此查询字符串:

  questions / search?content = foobar / 4 
pre>

表示 $ _ GET ['content'] ='foobar / 4';



您需要将分页网址更改为以下内容:

 问题/搜索/ 4 /?content = foobar 

/



您必须从分页的 $ config ['base_url']中删除查询字符串

或者尝试这个未公开的功能:

  //加载分页类后
$ this-> pagination-> suffix ='{YOUR QUERY STRING}';

或者更好的方法是添加 $ config ['suffix'] = {YOUR QUERY STRING}'; 加载您的配置。这应该自动将查询字符串添加到每个链接的 href



还需要对您的配置区域进行一些调整: / p>

  //确保对这些
// $ config ['first_link'] ='< ;
$ config ['first_link'] ='& lt;& lt;';
// $ config ['last_link'] ='>>';
$ config ['last_link'] ='& gt;& gt;';

$ offset = $ this-> uri-> segment(3);
//默认的URI段是3,它是你在上面使用的。删除此。
// $ config ['uri_segment'] = 2;

//这应该是FALSE(默认)。去掉它。
// $ config ['page_query_string'] = TRUE;

//如果你是
//使用URI段作为你的OFFSET,这应该是FALSE(默认)。去掉它。
// $ config ['use_page_numbers'] = TRUE;

//添加您的查询字符串
$ config ['suffix'] ='?content ='。
$ config ['base_url'] = base_url()。'questions / search /';
$ this-> pagination-> initialize($ config);


Im trying to paginate results returned by DB. But when I try to get the offset from URI:

questions/search?content=foobar/4

/4 should be the offset but it is assigned to the $_GET value.

This is the whole method in the controller:

http://pastebin.com/QFJddMDJ

$results = $this->question->search_results_count($content);
$this->load->library('pagination');
$config['total_rows'] = count($results);
$offset = $this->uri->segment(3);
if ($offset == false) $offset = 0;
$config['full_tag_open'] = '<ul class="pages">';
$config['full_tag_close'] = '</ul>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li><a  class="active">';
$config['cur_tag_close'] = '</a></li>';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '<li>';
$config['next_tag_open'] = '<li class="next">';
$config['next_tag_close'] = '</li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['first_link'] = '<<';
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['last_link'] = '>>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['per_page'] = 1;
$config['uri_segment'] = 2;
$config['page_query_string'] = TRUE;
$config['use_page_numbers'] = TRUE;
$config['suffix'] = '?content='.$content;
$config['base_url'] = base_url().'questions/search/';
$this->pagination->initialize($config);

解决方案

As I'm sure you know, URIs don't work like that. The query string must be at the end (or before a # hash fragment). This query string:

questions/search?content=foobar/4

Means $_GET['content'] = 'foobar/4';

You need to change your pagination URLs to something like this:

questions/search/4/?content=foobar

The / after the 4 there is also optional.

You'll have to remove the query string from your pagination's $config['base_url'] and instead append it to the links in the view, which sadly involves hacking the pagination class...

Or try this undocumented feature:

// After loading the pagination class
$this->pagination->suffix = '{YOUR QUERY STRING}';

Or better yet, just add $config['suffix'] = '{YOUR QUERY STRING}'; to your config before loading the class. This should automatically add the query string to every link's href.

Some adjustments to your config area also needed:

// Make sure to encode these
// $config['first_link'] = '<<';
$config['first_link'] = '&lt;&lt;';
// $config['last_link'] = '>>';
$config['last_link'] = '&gt;&gt;';

$offset = $this->uri->segment(3);
// Default URI segment is 3, and it's what you use above. Remove this.
// $config['uri_segment'] = 2;

// This should be FALSE (default). Remove it.
// $config['page_query_string'] = TRUE;

// This should be FALSE (default) if you're
// using the URI segment as your OFFSET. Remove it.
// $config['use_page_numbers'] = TRUE;

// Add your query string
$config['suffix'] = '?content='.$content;
$config['base_url'] = base_url().'questions/search/';
$this->pagination->initialize($config);

这篇关于在uri - Codeigniter中获取GET数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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