Codeigniter搜索引擎BUG:结果分页项显示所有帖子 [英] Codeigniter search engine BUG: results pagination items display ALL the posts

查看:40
本文介绍了Codeigniter搜索引擎BUG:结果分页项显示所有帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Codeigniter 3.1.8开发基本的博客应用程序。和Bootstrap4。该应用程序具有搜索帖子功能。

I am working on a basic blog application with Codeigniter 3.1.8. and Bootstrap 4. The application has a search posts functionality.

标题视图中的表单:

<form method="get" action="<?php echo base_url('posts/search') ?>" id="search_form" class="w-100 py-1 px-2 px-md-3 px-lg-5" accept-charset="utf-8">
    <div class="input-group <?php if(form_error('search')) echo 'has-error';?>">
      <input class="form-control form-control-dark" type="text" name="search" placeholder="Search posts..." aria-label="Search">
      <?php if(form_error('search')) echo form_error('search'); ?> 
      <div class="input-group-append">
        <button class="btn btn-success" type="submit"><i class="fa fa-search"></i></button>
      </div>
    </div>
</form>

由于帖子是分页的,因此搜索结果也应如此。 index() search()方法都是同一个Posts控制器的一部分,因此我尝试不重复分页代码:

Since the posts are paginated, so should the search results. The index() and search() methods are both part of the same Posts controller, so I have tried to NOT repeat the pagination's code:

private function _initPagination($path, $totalRows, $query_string_segment = 'page') {
    //load and configure pagination 
    $this->load->library('pagination');
    $config['base_url'] = base_url($path);
    $config['query_string_segment'] = $query_string_segment; 
    $config['total_rows'] = $totalRows;
    $config['per_page'] = 12;
    if (!isset($_GET[$config['query_string_segment']]) || $_GET[$config['query_string_segment']] < 1) {
      $_GET[$config['query_string_segment']] = 1;
    }
    $this->pagination->initialize($config);

    $limit = $config['per_page'];
    $offset = ($this->input->get($config['query_string_segment']) - 1) * $limit;

    return ['limit' => $limit, 'offset' => $offset];
  }

  public function index() {
    //call initialization method
    $config = $this->_initPagination("/posts", $this->Posts_model->get_num_rows());

    $data = $this->Static_model->get_static_data();
    $data['categories'] = $this->Categories_model->get_categories();

    //use limit and offset returned by _initPaginator method
    $data['posts'] = $this->Posts_model->get_posts($config['limit'], $config['offset']);
    $this->load->view('partials/header', $data);
    $this->load->view('posts');
    $this->load->view('partials/footer');
  }

  public function search() {
   // Force validation since the form's method is GET
   $this->form_validation->set_data($this->input->get());
   $this->form_validation->set_rules('search', 'Search term', 'required|trim|min_length[3]');
   $this->form_validation->set_error_delimiters('<p class = "error search-error"> ', ' </p>
       ');
    // If search fails
   if ($this->form_validation->run() === FALSE) {
       return $this->index();
   } else {
       $expression = $this->input->get('search');
       $posts_count = $this->Posts_model->search_count($expression);
       $query_string_segment = 'search=' . $expression . '&page';
       $config = $this->_initPagination("/posts/search", $posts_count, $query_string_segment);
       $data = $this->Static_model->get_static_data();
       $data['categories'] = $this->Categories_model->get_categories();
       //use limit and offset returned by _initPaginator method
       $data['posts'] = $this->Posts_model->search($expression, $config['limit'], $config['offset']);
       $data['expression'] = $expression;
       $data['posts_count'] = $posts_count;
       $this->load->view('partials/header', $data);
       $this->load->view('search');
       $this->load->view('partials/footer');
   }
} 

模型:

public function search_count($expression) {
    $query = $this->db->like('title', $expression)
                      ->or_like('description', $expression)
                      ->or_like('content', $expression);
    $query = $this->db->get('posts');
    return $query->num_rows();  
}

public function search($expression, $limit, $offset) {
    $query = $this->db->like('title', $expression)
                        ->or_like('description', $expression)
                        ->or_like('content', $expression);
    $this->db->order_by('posts.id', 'DESC');
    $query = $this->db->get('posts', $limit, $offset);
    return $query->result();
}

问题

搜索结果的第一页显示包含搜索表达式预期的的12个项目,而其他所有页面再次显示所有帖子 >。

The first page of search results displays 12 items containing the search expression, as expected, but all the other pages, display all the posts again.

由于 $ config ['query_string_segment'] 由分页使用,因此行 $ query_string_segment ='搜索='。 $ expression。 '& page'; 应该有助于通过 $ _ GET [] 搜索表达式传递到页面1 ,2等。

Since $config['query_string_segment'] is used by the pagination, the line $query_string_segment = 'search=' . $expression . '&page'; should help pass the search expression, via $_GET[], to the pages 1, 2 and so on.

但是页面项目链接不会保留 = &

But the page items link does not keep = and &:

<a href="http://localhost/ciblog/posts/search?search%3Dharum%26page=2" data-ci-pagination-page="2">2</a>

为什么会这样?我的错误在哪里?

Why does that happen? Where is my mistake?

推荐答案

_initPagination 方法内添加这2行解决了问题:

Adding this 2 lines inside _initPagination method solved the problem:

$config['enable_query_strings'] =TRUE;
$config['reuse_query_string'] =TRUE;

整个代码:

private function _initPagination($path, $totalRows, $query_string_segment = 'page') {
//load and configure pagination 
    $this->load->library('pagination');
    $config['base_url'] = base_url($path);
    $config['query_string_segment'] = $query_string_segment; 
    $config['enable_query_strings'] =TRUE;
    $config['reuse_query_string'] =TRUE;
    $config['total_rows'] = $totalRows;
    $config['per_page'] = 12;
    if (!isset($_GET[$config['query_string_segment']]) || $_GET[$config['query_string_segment']] < 1) {
        $_GET[$config['query_string_segment']] = 1;
    }
    $this->pagination->initialize($config);

    $limit = $config['per_page'];
    $offset = ($this->input->get($config['query_string_segment']) - 1) * $limit;

    return ['limit' => $limit, 'offset' => $offset];
}

public function search() {
   // Force validation since the form's method is GET
    $this->form_validation->set_data($this->input->get());
    $this->form_validation->set_rules('search', 'Search term', 'required|trim|min_length[3]');
    $this->form_validation->set_error_delimiters('<p class = "error search-error"> ', ' </p>
        ');
    // If search fails
    if ($this->form_validation->run() === FALSE) {
        return $this->index();
    } else {
        $expression = $this->input->get('search');
        $posts_count = $this->Posts_model->search_count($expression);
        $query_string_segment = 'page';
        $config = $this->_initPagination("/posts/search", $posts_count, $query_string_segment);
        $data = $this->Static_model->get_static_data();
        $data['categories'] = $this->Categories_model->get_categories();
   //use limit and offset returned by _initPaginator method
        $data['posts'] = $this->Posts_model->search($expression, $config['limit'], $config['offset']);
        $data['expression'] = $expression;
        $data['posts_count'] = $posts_count;
        $this->load->view('partials/header', $data);
        $this->load->view('search');
        $this->load->view('partials/footer');
    }
} 

这篇关于Codeigniter搜索引擎BUG:结果分页项显示所有帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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