Using pagination with query string for a search form which has the method set to get in codeigniter(对搜索表单使用带有查询字符串的分页,该表单的方法设置为在 codeigniter 中获取)
问题描述
我正在敲击键盘,试图找出一种使用带有分页的查询字符串的方法,一切正常,直到 FIRST 页面链接出现.
I'm banging my head on the keyboard trying to figure out a way to use query string with pagination every thing works fine until FIRST page link appears.
所有其他链接的末尾都附加了查询字符串,但 First 页面链接 缺少查询字符串
All other links have the query string appended to their end but the First page link misses the query string
其他页面的链接:
http://localhost/index.php/search/index/9?q=some_Data_From_Form
第一个页面链接显示了我在 $config['base_url'] 中设置的链接变量:
The FIRST page link show the link that I've set in the $config['base_url']
variable:
http://localhost/index.php/search/index/
搜索表单:
$attributes=array('id'=>'search','class'=>'clearfix','method'=>'get');
echo form_open(base_url().'index.php/search/index',$attributes);
它有一个名称设置为 q 的文本框.
It has a textbox with the name set to q.
我在 stackoverflow 上偶然发现了一些答案/示例,这就是我写的:
I stumbled upon a few answers/examples on stackoverflow and this is what I wrote:
分页配置文件有
$config['per_page'] = '1';
$config['uri_segment'] = '3';
以及其他像 num_tag_open 等
控制器类:
class Search extends CI_Controller {
public function Search(){
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('input');
$this->load->model('blog_model');
$this->load->library('pagination');
$this->config->load('pagination'); //other pagination related config variables
}
public function index($page=0){
$q = trim($this->input->get('q'));
if(strlen($q)>0){
//validate input and show data
$config['enable_query_strings']=TRUE;
$getData = array('q'=>$q);
$config['base_url'] = 'http://localhost/index.php/search/index/';
$config['suffix'] = '?'.http_build_query($getData,'',"&");
$data['rows'] = $this->blog_model->getBySearch($q,$this->config->item('per_page'),$page);
if(empty($data['rows'])){
//no results found
}else{
//match found
$config['total_rows'] = $this->blog_model->getBySearchCount($q);
$this->pagination->initialize($config);
$link->linkBar = $this->pagination->create_links();
$this->load->view('myview',array($data,$link));
}
}else if(strlen($q)==0){
//warn user for the missing query and show a searchbox
}
}
}
求救!伙计们,请帮帮我
S.O.S! Guys, please help me out
推荐答案
我简直不敢相信,我花了几个小时在网上搜索解决方案!它一直在我身边.我应该在发布这个问题之前打开分页库并查看其内容.只需一行,问题就解决了.
我在 index 方法中添加了以下行.$config['first_url'] = '/index.php/search/index/?q='.$q;
I can't believe this, I spent hours searching the web for a solution !
It was always with me.I should have opened the pagination library and viewed its content before I posted this question.Just one line and problem solved.
I added the following line in the index method.
$config['first_url'] = '/index.php/search/index/?q='.$q;
这篇关于对搜索表单使用带有查询字符串的分页,该表单的方法设置为在 codeigniter 中获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:对搜索表单使用带有查询字符串的分页,该表单的方法设置为在 codeigniter 中获取
基础教程推荐
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
