Codeigniter搜索分页

时间:2012-05-06 13:28:30

标签: php codeigniter url search pagination

我正在研究一些搜索引擎,我希望有一个很好的分页,但它看起来不适用于POST类型。我有一些表格,其中输入查询进行搜索,表格发出后,有一些结果。结果的第一页通常正常工作,但是如果我点击我的分页中的任何其他页面都没有结果,因为POST输入值没有存储。我已经尝试使用GET进行搜索 - 在URL中添加查询,但这也是不可能的,因为CodeIgniter不允许在urlencode中使用某些字符。我也在寻找使用base64对查询字符串进行编码,但它看起来也没办法,因为再次有一些不允许的字符。

我需要做什么而不是所有这些?

2 个答案:

答案 0 :(得分:1)

您可以将POST数据转换为GET数据(例如,作为URL参数,请参阅Query part of the URI specificationVariables From External Sources in the PHP manual),或者将搜索存储到会话中并仅传递搜索标识符以便您可以从会话中检索搜索的POST数据。

答案 1 :(得分:0)

Search with Pagination in Codeingter

Step 1 : Created search_results.php in view
  <?php include ('public_header.php');?>
  <div class="container">
   <h1>Serach Results</h1>
   <table class="table">
    <thead>
     <tr>
      <td>Sr No.</td>
      <td>Article Title</td>
      <td>Published On</td>
     </tr>
    </thead>
    <tbody>
     <tr>
    <?php if(count($articles)):?>
     <?php $count=$this->uri->segment(4,0);?>
     <?php foreach($articles as $article ):?>
      <td><?= ++$count?></td>
      <td><?= $article->title?></td>
      <td><?= "Date" ?></td>
     </tr>
     <?php endforeach;?>

    <?php else: ?>
     <tr>
      <td colspan="3"> No Records Found.</td>
     </tr>
     <?php endif;?>
    </tbody>
   </table>
   <?= $this->pagination->create_links();?>
  </div>

  <?php include ('public_footer.php');?>

step 2 created function in controller

  public function search()
  {
   $this->load->library('form_validation');
   $this->form_validation->set_rules('query','Query','required');

   if(! $this->form_validation->run())
    $this->index();

   $query=$this->input->post('query');
   return redirect("user/search_results/$query");

   // $this->load->view('public/search_results',compact('articles'));
  }

  public function search_results($query)
  {
   $this->load->helper('form');
   $this->load->model('articlesmodel','articles');
   $this->load->library('pagination');
   $config=[
     'base_url'  => base_url("user/search_results/$query"),
     'per_page'   => 5,
     'total_row'  => $this->articles->count_search_results($query),
     'full_tag_open' => "<ul class='pagination'>",
     'full_tag_close'=> "</ul>",
     'first_tag_opne' => "<li>",
     'first_tag_close'=> "</li>",
     'uri_segment'  => 4,
     // 'first_link'  => "<li>"
     // 'last_link'   => "</li>"
     'last_tag_opne' => "<li>",
     'last_tag_close' => "</li>",
     'next_tag_opne' => "<li>",
     'next_tag_close' => "</li>",
     'pre_tag_open'  => "<li>",
     'pre_tag_close' => "</li>",
     'num_tag_open'  => '<li>',
     'num_tag_close' => '</li>',
     'cur_tag_open'  => "<li class='active'><a>",
     'cur_tag_close' => '</a></li>',
   ];

   $this->pagination->initialize($config);
   $articles=$this->articles->search($query,$config['per_page'],$this->uri->segment(4));
   $this->load->view('public/search_results',compact('articles'));
  }

Step 3 :Created function in model

  public function search($query,$limit,$offset)
  {
   $q=$this->db->from('articles')
      ->like('title',$query)
      ->limit($limit,$offset)
      ->get();
   return $q->result();
  }
  public function count_search_results($query)
  {
   $q=$this->db->from('articles')
     ->like('title',$query)
     ->get();
   return $q->num_rows();
  }