搜索不工作在codeigniter与分页 [英] Search does not works in codeigniter with pagination

查看:112
本文介绍了搜索不工作在codeigniter与分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的清单中新增搜寻。我使用codeigniter和分页列出。搜索只是将数据库搜索作为样本ID。搜索在分页的第一页完美。但是当涉及到其他页面它不工作。

I am trying to add search in my lists. I am using codeigniter and pagination for listing. The search is just the database search as a sample id . The search works perfect in the first page of pagination. But when it comes to other pages it does not work.

这是我的控制器的代码。

This is the code for my controller.

  $this->clear_sample_id_catche();
  $out=$this->Mgeneral->listData();
  $per_page = 10;
  $total = $this->Mgeneral->countData();
  $data = $this->Mgeneral->listData($per_page, $this->uri->segment(3));

   $base_url = site_url('general/listData');

   $config['base_url'] = $base_url;
   $config['total_rows'] = $total;
   $config['per_page'] = $per_page;
   $config['uri_segment'] = '3';

  $this->pagination->initialize($config); 
  $data=array('body'=>'list','out'=>$data);

  $this->load->view('template',$data);

我添加了表单以在我的视图中搜索。
任何一个建议我的问题是什么?
提前感谢。

I added form to search in my view. Can any one suggest my what the problem is?? Thanks in advance.

推荐答案

您必须忘记传递分页的搜索查询。看到页面号是查询字符串的第三段,我猜想当用户提交时,搜索表单将执行 POST 操作。在用户提交表单后,当然会工作。因为他们只是执行了一个 POST 请求,并且通过使用 $ _ POST 获取搜索查询> $ this-> input-> post('user_input')或类似的东西。但是,当他们试图通过点击页面链接(这是一个 GET 请求)导航到另一个页面时,搜索失败,因为超级全局 $ _ POST 数组为空。

You must be forgetting to pass the search query for the pagination. Seeing how the page number is the third segment of the query string, I'm guessing that the search form will do a POST action when it gets submitted by the user. After the user submitted the form, of course it will work. Because they just did a POST request, and you fetch the search query from $_POST by using $this->input->post('user_input') or something similar. But then, when they try to navigate to another page by clicking on the page number link (which is a GET request), the search fails because the superglobal $_POST array is empty.

解决这个问题的一个解决方案是设置一个会话值(我建议你使用CodeIgniter的Session Class ),而不是使用来自 $ _ POST 的搜索查询,而使用会话中存储的查询。

A solution to solve this problem is to set a session value (I recommend that you use CodeIgniter's Session Class) based on the user input and instead of using the search query from $_POST, and use the one stored in session instead.

另一种方法是为搜索结果创建另一个页面(假设它当前与表单位于同一页面,即在同一个控制器函数中)。然后使搜索表单做一个 GET 请求到自己的控制器函数,然后它将编码 base64 中的搜索查询,并用替换编码字符串中的 = (等号)。之后,将用户重定向到搜索结果页面,同时将编码搜索查询作为URL查询字符串中的第三个段传递(页码将是第四个)。并在该函数中将替换回 = ,并使用 base64_decode c $ c>获取搜索查询并将其传递给将生成搜索结果的函数。

Another way you can do it, is to make another page for the search result (assuming it is currently in the same page as the form, i.e. in the same controller function). Then make the search form do a GET request to its own controller function which will then encode the search query in base64, and replace the = (equal sign) from the encoded string with a .. After that, redirect the user to the search result page while passing the encoded search query as the third segment in the URL query string (the page number will be the fourth). And in that function replace the . back to = and use base64_decode() to get the search query and pass it to the function that will generate the search result.

当然,还有很多其他方法来实现结果。只要google吧。

Of course, there are lots of other ways to achieve the same result. Just google it.

这篇关于搜索不工作在codeigniter与分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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