1

我正在研究一些搜索引擎,我希望有一个不错的分页,但看起来这与 POST 类型不符。我有一些表单,其中输入查询以进行搜索,发送表单后,有一些结果。结果的第一页正常工作,但是如果我单击分页中的任何其他页面,则没有结果,因为 POST 输入值没有存储在任何地方。我已经尝试使用 GET 进行搜索,例如在 URL 中添加查询,但这也是不可能的,因为 CodeIgniter 不允许某些 urlencode 中的字符。我也在寻找使用 base64 对查询字符串进行编码,但看起来这也是不可能的,因为又出现了一些不允许的字符。

我必须做什么来代替所有这些?

4

2 回答 2

1

您可以将 POST 数据转换为 GET 数据(例如,作为 URL 参数,请参阅URI 规范的查询部分PHP 手册中的外部源变量),或者将搜索存储到会话中并仅传递搜索标识符,以便您可以从会话中检索搜索的 POST 数据。

于 2012-05-06T13:45:51.247 回答
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();
  }
于 2018-08-08T11:36:39.867 回答