对查询表单使用分页,查询字符串的方法设置为在codeigniter中获取 [英] Using pagination with query string for a search form which has the method set to get in codeigniter

查看:187
本文介绍了对查询表单使用分页,查询字符串的方法设置为在codeigniter中获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰碰我的脑袋在键盘上试图找出一种方法使用查询字符串与分页每一件事情都很好,直到 FIRST 页面链接出现。 >

所有其他链接都附加了查询字符串,但第一页链接错过了查询string



其他页面的链接:

  http://localhost/index.php/search/index/9?q = some_Data_From_Form 

第一页链接显示了我在 $ config ['base_url']
变量中设置的链接:

  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



我在stackoverflow上发现了几个答案/例子,这是我写的:



Pagination配置文件包含

  $ config ['per_page'] ='1' 
$ config ['uri_segment'] ='3';

而其他人喜欢 num_tag_open p>

控制器类:

  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'); //其他分页相关的配置变量
}

public function index($ page = 0){
$ q = trim($ this-> input-> get 'q'));
if(strlen($ q)> 0){
//验证输入并显示数据
$ config ['enable_query_strings'] = TRUE;
$ getData = array('q'=> $ q);
$ config ['base_url'] ='http://localhost/index.php/search/index/';
$ config ['suffix'] ='?'。http_build_query($ getData,'',& amp;);

$ data ['rows'] = $ this-> blog_model-> getBySearch($ q,$ this-> config-> item('per_page'),$ page);
if(empty($ data ['rows'])){
//没有找到结果

} else {
//匹配找到
$ 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){
//警告用户缺少查询并显示搜索框

}
}
}

SOS!请帮我出来

解决方案

我不敢相信,我花了几个小时在网上寻找解决方案!
它总是与我。我应该打开分页库,并在我发布这个问题之前查看其内容。只有一行,问题解决。

我在索引方法中添加了以下行。

$ config ['first_url'] ='/index.php/search/index/?q='.$q;


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.

All other links have the query string appended to their end but the First page link misses the query string

Links for other pages:

http://localhost/index.php/search/index/9?q=some_Data_From_Form

The FIRST page link show the link that I've set in the $config['base_url'] variable:

http://localhost/index.php/search/index/

The search form:

$attributes=array('id'=>'search','class'=>'clearfix','method'=>'get');
echo form_open(base_url().'index.php/search/index',$attributes);

It has a textbox with the name set to q.

I stumbled upon a few answers/examples on stackoverflow and this is what I wrote:

The Pagination configuration file has

$config['per_page'] = '1';
$config['uri_segment'] = '3';

and others like num_tag_open etc.

The controller class:

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

解决方案

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中获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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