【问题标题】:CodeIgniter pagination not working for get methodCodeIgniter 分页不适用于 get 方法
【发布时间】:2014-04-13 07:51:12
【问题描述】:

我需要使用获取参数进行分页。在方法中,我编写了如下代码。

public function index($offset = 0) {

    $data['title_for_layout'] = 'Apartment Advertisement - Search';


    // Pagination config
    $this->load->library('pagination');

    $pagination['enable_query_strings'] = true;
    $pagination['page_query_string'] = true;
    $pagination['allow_get_array'] = true;
    $pagination['first_url'] = base_url('search');
    $pagination['base_url'] = $config['first_url'];
    $pagination['total_rows'] = $this->aa_movies->no_of_search_movies();
    $data['no_of_results'] = $pagination['total_rows'];
    $pagination['use_page_numbers'] = TRUE;
    $pagination['per_page'] = 20;

    // Initialize pagination
    $this->pagination->initialize($pagination);

    // Offset
    $offset = 20 * $offset;

    // Get apartments
    $data['apartments'] = $this->aa_movies->search_movies($this->input->get('keyword'), $this->input->get('city'), $this->input->get('locations'), $offset);

    $this->load->view($this->config->item('index_search_view'), $data);
}

在视图中

<?php echo form_open(base_url('search'), array('method' => 'get'); ?>
<?php echo $this->pagination->create_links(); ?>
<?php echo form_close(); ?>

所以分页网址看起来像

http://localhost/reelstubs/search/1

但我想在分页网址如下

http://localhost/reelstubs/search?keyword=complex&city=bangalore&page=1

即使我尝试通过包含 $_SERVER['REQUEST_URI'] 来更改分页 base_url。但网址看起来像

http://localhost/reelstubs/search?keyword=complex&city=bangalore/1

所以我没有使用$this-&gt;uri-&gt;segment(); 获得页面,请帮助我克服这个问题。我已经浪费了2天了。这项工作将受到更多赞赏。

【问题讨论】:

  • 不要使用$this-&gt;uri-&gt;segment();,尝试类似 thsi $this-&gt;input-&gt;get("page")

标签: codeigniter pagination codeigniter-2 codeigniter-url


【解决方案1】:
  1. 您可以在隐藏输入中添加过滤字符串(关键字和城市),并使用 $this->input->get('name')

  2. 获取这些值
  3. 或者您可以在段中发送必要的值并使用 $this->uri->segment();

    http://localhost/search/method(func)/complex/bangalore/1

    $keyword = $this->uri->segment('3');
    $city = $this->uri->segment('4');
    $page = $this->uri->segment('5');

【讨论】:

    【解决方案2】:

    使用CodeIgniter 3.0 版本的分页库。它有一个配置选项来重用查询字符串。

    我自己在 CodeIgniter 2 中实现了它,但我没有替换分布式版本,而是将它部署为一个名为 MY_Pagination 的重载库,并将其放在我的“应用程序/库”文件夹中。为了以这种方式工作,我必须做的唯一代码更改是将访问修饰符设置为公共而不是受保护。

    【讨论】:

      猜你喜欢
      • 2020-03-12
      • 2013-05-23
      • 1970-01-01
      • 2018-04-22
      • 1970-01-01
      • 2016-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多