用户
 找回密码
 入住 CI 中国社区
搜索
查看: 1265|回复: 2
收起左侧

[版本 3.x] 搜索结果分页参数传递得问题

[复制链接]
发表于 2016-7-29 13:09:38 | 显示全部楼层 |阅读模式
最近改了一款国外得壁纸系统,发现搜索关键词 结果得下一页不能传递参数过去
本来应该是search?q=%E7%BE%8E%E5%A5%B3&page=2
结果下一页是search?page=2
各位大神帮我看看
代码如下:
PHP复制代码
 
<?php defined('BASEPATH') OR exit('No direct script access allowed');
 
class Search_private extends wsFrontend
{
 
    public function __construct()
    {
        parent::__construct();
        if (strpos(strtolower($this->uri->uri_string()), 'search_private') !== false) {
            redirect();
        }
        $this->db->cache_set_path(dirname($this->db->cachedir) . '/Search_page/');
    }
 
    public function index()
    {
        $this->tpl->hide_search = true;
        $data['url'] = $this->route_links->get('search_page');
        $this->load->model(array('wallpaper_model', 'category_model'));
 
        $filter['show_disabled'] = FALSE;
        $filter['per_page'] = $config['per_page'] = $this->config->item('items_per_page');
        $page_number = (int)$this->input->get('page');
        $filter['offset'] = ($page_number ? ($page_number - 1) * $config['per_page'] : 0);
        $filter['bypath'] = false;
 
        $data['keyword'] = trim($this->input->get('q'));
 
        if ($data['keyword'])
            $filter['name'] = $data['keyword'];
 
        $this->db->custom_seqgment('search', 'index');
        $data['categories'] = $this->category_model->get_categoryDropDownTree();
        $this->db->reset_custom_seqgment();
 
 
        if ($this->input->get('category') && array_key_exists($this->input->get('category'), $data['categories']))
            $filter['category'] = $this->input->get('category');
 
        if (!empty($_GET['sort']))
            $filter['sort'] = $_GET['sort'];
 
        if (isset($_GET['time']))
            $filter['time'] = $_GET['time'];
 
        if (isset($_GET['color'])) {
            $color = $_GET['color'];
            $this->load->library('colors_tool');
            if (ctype_xdigit($color) && strlen($color) == 6) {
                $color_tool = new colors_tool($color);
                $filter['colors'] = $color_tool->similar_colors_by_hex(40, 20);
                if (empty($_GET['sort'])) {
                    $_GET['sort'] = $filter['sort'] = 'colors';
                }
            }
        }
                $data['colors'] = trim($this->input->get('colors'));
 
        $data['total_count'] = $this->wallpaper_model->getWallpapers($filter + array('count' => true));
 
        //Initialize Pagination
        $config['base_url'] = $data['url'];
        $config['total_rows'] = $data['total_count'];
        $config['custom_query_string'] = TRUE;
        $config['page_query_string'] = TRUE;
                $config['use_page_numbers'] = TRUE;
        $config['query_string_segment'] = 'page';
 
        if (!empty($_GET)) {
            $get_method = $this->frontend_helper->get_query_string(array('page'));
            $config['prefix'] = ($get_method ? $get_method . '&' : '?') . 'page=';
            $config['first_url'] = $config['base_url'] . $get_method;
        } else {
            $config['prefix'] = '?page=';
        }
 
        $this->load->library('pagination');
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $data['wallpapers'] = $this->wallpaper_model->getWallpapers($filter);
 
 
        $this->tpl->breadcrumbs['壁纸搜索'] = $data['url'];
        $this->tpl->breadcrumbs[' 搜索到 ' . ($data['keyword'] ? $data['keyword'] : '相关').' 约有'.$data['total_count'].'张壁纸'] = 'active';
 
        $SearchReplace = array(
            '{keyword}' => ($data['keyword'] ? $data['keyword'] : '相关')
        );
 
        $this->tpl->meta ['title'] = $this->metadata->get_metadata('_search_index_title', 'search_page', $SearchReplace, false, $page_number);
        $this->tpl->meta ['description'] = $this->metadata->get_metadata('_search_index_description', 'search_page', $SearchReplace, false, $page_number);
        $this->tpl->meta ['keywords'] = $this->metadata->get_metadata('_search_index_keywords', 'search_page', $SearchReplace, false, $page_number);
 
 
        $this->tpl->contents [] = $this->tpl->fetch('contents/search_index', $data);
        $this->tpl->render('layouts/empty');
    }
 
}
 
 
复制代码

演示地址:http://www.6x.nz/search?q=%E7%BE%8E%E5%A5%B3
发表于 2016-7-29 13:58:10 | 显示全部楼层
问题出在 pagination的配置上,  $config['base_url'] = $data['url'];
这里的base_url需要带上查询参数,也就是q=keyword, pagination只会带基本的地址,不会带query string.
 楼主| 发表于 2016-7-29 15:04:16 | 显示全部楼层
yuzhigang5460 发表于 2016-7-29 13:58
问题出在 pagination的配置上,  $config['base_url'] = $data['url'];
这里的base_url需要带上查询参数, ...

谢谢你得回答,我已经搞定
PHP复制代码
[color=#0088]$get_method = $this->frontend_helper->get_query_string(array('page'));
复制代码

改为

PHP复制代码
$get_method = '/?' . http_build_query($this->input->get());
复制代码


本版积分规则