复杂的搜索功能和分页

时间:2018-12-07 05:26:37

标签: php codeigniter codeigniter-3

我正在构建一个搜索功能,需要在其中创建一个联接,如果行数超过X则从那里显示分页。

这是我控制器中的代码:

public function searchPosts()
{
    // Get Inputs
    $title      = $this->input->get('title');
    $featured   = $this->input->get('featured');
    $free       = $this->input->get('free');
    $userID     = $this->input->get('user_id');
    $keyword    = $this->input->get('keyword');
    $price_low  = $this->input->get('price_low');
    $price_high = $this->input->get('price_high');
    $minimum    = str_replace( '.', '', $price_low );
    $maximum    = str_replace( '.', '', $price_high );

    // Pagination
    $config['base_url']         = base_url('store/searchPosts');
    $config['total_rows']       = $this->PublicPagination_model
                                        ->getStoreSearchCount($title, 
                                            $featured, $free, $userID, 
                                            $keyword, $minimum, $maximum);
    $config['per_page']         = 9;
    $config['uri_segment']      = 3;
    $config['full_tag_open']    = '<ul class="pagination">';
    $config['full_tag_close']   = '</ul>';
    $config['attributes']       = array('class' => 'page-link');
    $config['first_link']       = 'First';
    $config['last_link']        = 'Last';
    $config['first_tag_open']   = '<li>';
    $config['first_tag_close']  = '</li>';
    $config['prev_link']        = '&laquo';
    $config['prev_tag_open']    = '<li class="prev">';
    $config['prev_tag_close']   = '</li>';
    $config['next_link']        = '&raquo';
    $config['next_tag_open']    = '<li>';
    $config['next_tag_close']   = '</li>';
    $config['last_tag_open']    = '<li>';
    $config['last_tag_close']   = '</li>';
    $config['cur_tag_open']     = '<li class="page-item active">
                                    <a href="#" class="page-link">';
    $config['cur_tag_close']    = '<span class="sr-only">(current)</span>
                                    </a></li>';
    $config['num_tag_open']     = '<li>';
    $config['num_tag_close']    = '</li>';

    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $this->pagination->initialize($config);
    $data['pagination'] = $this->pagination->create_links();

    $limit  =   $config["per_page"];
    $data['posts'] = $this->PublicPagination_model
                            ->getStoreSearchCountLimit($title, $featured,
                                        $free, $userID, $keyword, $minimum, 
                                        $maximum, $limit, $page);

    // Load template
    $this->template->load('public', 'default', 'store/search', $data);
}

这是我的模型:

public function getStoreSearchCount($title, $featured, $free, 
                                    $userID, $keyword, $minimum, 
                                    $maximum)
{
    $this->db->select('COUNT(*) as num_row');
    $this->db->from($this->posts);
    $this->db->join('ci_relationship', 
                    'ci_relationship.post_id = ci_posts.post_id', 'INNER');
    $this->db->order_by('ci_relationship.post_id', 'DESC');
    $this->db->group_by('ci_relationship.post_id');
    $this->db->like('title', $title);
    $this->db->or_like('body', $keyword);
    $this->db->where('ci_relationship.user_id', $userID);
    $this->db->where('ci_relationship.type', 'product');
    $this->db->where('ci_relationship.featured', $featured);
    $this->db->where('ci_relationship.free', $free);
    $this->db->where('ci_relationship.price >', $minimum);
    $this->db->where('ci_relationship.price <', $maximum);
    $this->db->where('ci_relationship.status', $this->published);
    $query = $this->db->get();
    $result = $query->result();

    return $result[0]->num_row;
}

public function getStoreSearchCountLimit($featured, $free, $minimum, 
                                        $maximum, $title, $userID, 
                                        $keyword, $limit, $start)
{
    $this->db->select('*');
    $this->db->from($this->posts);
    $this->db->join('ci_relationship', 
                    'ci_relationship.post_id = ci_posts.post_id', 'INNER');
    $this->db->order_by('ci_relationship.post_id', 'DESC');
    $this->db->group_by('ci_relationship.post_id');
    $this->db->like('title', $title);
    $this->db->or_like('body', $keyword);
    $this->db->where('ci_relationship.user_id', $userID);
    $this->db->where('ci_relationship.type', 'product');
    $this->db->where('ci_relationship.featured', $featured);
    $this->db->where('ci_relationship.free', $free);
    $this->db->where('ci_relationship.price >', $minimum);
    $this->db->where('ci_relationship.price <', $maximum);
    $this->db->where('ci_relationship.status', $this->published);

    $this->db->limit($limit, $start);
    $query = $this->db->get();
        return $result = $query->result();
}

对于Internet上的解决方案,我已经寻找了无数次,但我只是找不到合适的解决方案。

比方说,我的搜索功能是一个过滤器,如下图所示: enter image description here

谢谢。

0 个答案:

没有答案