【问题标题】:CodeIgniter search input and pagination URLCodeIgniter 搜索输入和分页 URL
【发布时间】:2016-06-28 04:38:58
【问题描述】:

我想创建一个包含搜索表单字符串和分页页面的 URL

例如,我的基本 URL 是 example.com/search
显示搜索结果时为example.com/search/?s=keyword,显示下一页的搜索结果时,第二页为example.com/search/?s=term&p=2,依此类推

如何在 CodeIgniter 中做到这一点?

【问题讨论】:

  • 我不是 CodeIgniter 的大师,但你不能只检查查询字符串中的 p 参数,如果它不存在,而不仅仅是将值设置为1。根据 google 搜索,$this->input->get('p', TRUE); 将在字符串中获取 p 的值,而在 php 中,您不能将其包装在 if 语句中,然后继续前进吗?

标签: php codeigniter pagination


【解决方案1】:

试试这个。这里使用page而不是URL中的p

$keyword = trim($this->input->get('s', TRUE));
$this->load->library('pagination');
$config['total_rows'] = $this->db->get('table_name')->num_rows();
$config['per_page'] = 10;
$config['num_links'] = 5;
$config['enable_query_strings'] = TRUE;
$config['use_page_numbers'] = TRUE;
$config['query_string_segment'] = 'page';
$config['page_query_string'] = TRUE;
$config['base_url'] = site_url('search/index/?s=' . $keyword);
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
if ($this->input->get('page')) {
    $sgm = (int) trim($this->input->get('page'));
    $segment = $config['per_page'] * ($sgm - 1);
} else {
    $segment = 0;
}

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

// your query
$query = $this->db->select('*')->from('table_name')->limit($config['per_page'], $segment)->get();

现在您的网址将与您在问题中提到的相同

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-15
    • 2012-05-15
    • 2012-12-03
    • 2014-02-13
    • 1970-01-01
    • 2023-03-20
    • 2014-03-26
    • 1970-01-01
    相关资源
    最近更新 更多