Codeigniter Perpage链接不显示URL分页链接

时间:2016-10-05 10:49:58

标签: codeigniter

我正在使我的桌面链接可排序和可订购我的per_page如何不首先显示

http://localhost/project-1/forum/category/1/&order=asc?per_page=1

它应该显示为

http://localhost/codeigniter/forum/category/1/?per_page=1&order=asc

  

当我点击分页链接时,如果已经订购或排序的问题我怎么能确保当我点击分页链接时,即使已经订购/排序仍将总是显示?per_page首先在网址中。

$url = '';

if ($this->input->get('sort')) {
    $url .= '&sort=' . $this->input->get('sort');
}

if ($this->input->get('order')) {
    $url .= '&order=' . $this->input->get('order');
}

$config["base_url"] = base_url('forum/category') .'/'. $category_id .'/'.  $url;

控制器

<?php

class Forum extends MX_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('catalog/forum/forum_model');
        $this->load->library('pagination');
    }

    public function category() {
        $this->document->set_title('Forums');

        $category_id = $this->uri->segment(3);

        if ($this->input->get('sort')) {
            $sort = $this->input->get('sort');
        } else {
            $sort = 'message';
        }

        if ($this->input->get('order')) {
            $order = $this->input->get('order');
        } else {
            $order = 'asc';
        }

        $url = '';

        if ($this->input->get('per_page')) {
            $url .= '?per_page=' . $this->input->get('per_page');
        }

        if ($this->input->get('sort')) {
            $url .= '&sort=' . $this->input->get('sort');
        }

        if ($this->input->get('order')) {
            $url .= '&order=' . $this->input->get('order');
        }

        $config["base_url"] = base_url('forum/category') .'/'. $category_id .'/'.  $url;
        $config["total_rows"] = $this->forum_model->total_category($category_id);
        $config["per_page"] = 1;
        $config['page_query_string'] = TRUE;
        $config['full_tag_open'] = "<ul class='pagination'>";
        $config['full_tag_close'] ="</ul>";
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
        $config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
        $config['next_tag_open'] = "<li>";
        $config['next_tagl_close'] = "</li>";
        $config['prev_tag_open'] = "<li>";
        $config['prev_tagl_close'] = "</li>";
        $config['first_tag_open'] = "<li>";
        $config['first_tagl_close'] = "</li>";
        $config['last_tag_open'] = "<li>";
        $config['last_tagl_close'] = "</li>";

        $this->pagination->initialize($config);

        $start = ($this->input->get('per_page')) ? $this->input->get('per_page') : '';

        $filter_data = array(
            'limit' => $config["per_page"],
            'start' => $start,
            'category_id' => $category_id,
            'sort' => $sort,
            'order' => $this->input->get('order')
        );

        $data['threads'] = array();

        $results = $this->forum_model->get_threads_for_forum($filter_data);

        foreach ($results as $result) {
            $data['threads'][] = array(
                'thread_id' => $result['thread_id'],
                'user_id' => $result['user_id'],
                'username' => $result['username'],
                'subject' => $result['subject'],
                'link' => site_url('thread') . '-' . $result['thread_id'],
                'total' => $this->forum_model->total_threads($result['thread_id']),
                'date_created' => date('d-m-Y', strtotime($result['date_created'])),
                'user_link' => site_url('user') . '-' . $result['user_id']
            );
        }

        $data['header'] = Modules::run('catalog/common/header/index');
        $data['footer'] = Modules::run('catalog/common/footer/index');
        $data['menu'] = Modules::run('catalog/common/menu/index');
        $data['pagination'] = $this->pagination->create_links();
        $data['back'] = site_url('forum');
        $data['thread'] = site_url('newthread') . '?fid=' . $category_id;

        $url = '';

        if ($order == 'asc') {
            $url .= '?order=desc';
        } else {
            $url .= '?order=asc';
        }

        $data['message'] = site_url('forum/category') .'/'. $category_id .'/'. $url;
        $data['sort'] = $sort;
        $data['order'] = '';

        $this->load->view('default/template/forum/forum_thread_view', $data);
    }
}

2 个答案:

答案 0 :(得分:1)

如果网址不包含以下内容,请设置默认类别ID:

if ($this->uri->segment(3) == 'null') {
  $category_id = 1;  //set default for null
} else {
   $category_id = $this->uri->segment(3);
}
  

编辑你的config ['base_url']并添加set $ config ['suffix']如果$ _GET不是   空。

新的分页配置:

$config["base_url"] = base_url('forum/category'); //no need custom category id
if (count($_GET) > 0) 
    $config['suffix']='?'.http_build_query($_GET,'',"&");//to encode requested data
$config["per_page"] = 1;
$config['page_query_string'] = TRUE;
$config['full_tag_open'] = "<ul class='pagination'>";
$config['full_tag_close'] ="</ul>";
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
$config['next_tag_open'] = "<li>";
$config['next_tagl_close'] = "</li>";
$config['prev_tag_open'] = "<li>";
$config['prev_tagl_close'] = "</li>";
$config['first_tag_open'] = "<li>";
$config['first_tagl_close'] = "</li>";
$config['last_tag_open'] = "<li>";
$config['last_tagl_close'] = "</li>";

答案 1 :(得分:1)

我没有看到任何:

$config[‘reuse_query_string’] = TRUE;

这将允许使用混合类型的网址。这在CI 3.0之前是不可能的。