簡體   English   中英

uri中帶有搜索詞的Codeigniter分頁

[英]Codeigniter pagination with search term in uri

我有一個顯示所有記錄的控制器功能,我正在使用分頁類將記錄划分為頁面。 控制器看起來像這樣

class Example extends CI_Controller {

     [...]

     function All()
     {
       //Gets all the records
      //Pagination uri_segment set to 3
     }
}

現在,我添加了搜索記錄的功能。 我需要搜索詞位於uri中,因此該函數類似於function All($search_term = false) 我的問題是該功能可能有或沒有搜索詞。 並據此改變分頁的uri片段。 沒有搜索字詞uri_segment為3,值為4。

在使用相同功能並在網址中包含搜索字詞時,有沒有辦法解決此問題?

它可以通過多種方式完成,就我個人而言,我會做一些簡單的事情,例如將其存儲在會話或cookie中,但是您也可以執行URI,也可以參考CI論壇:
http://codeigniter.com/forums/viewthread/59364/

請記住,人們可能會在搜索字詞中鍵入非URI友好的字符串,並且您必須對值進行urlencode /解碼才能將其放入字符串中,而Cookie或會話則不需要這樣做。 但是我再次看到將鏈接復制並粘貼到某人以獲取搜索結果的好處。

您可以將get參數用作查詢字符串。 例如:

search.php

function search() {
  $q = $this->input->get('q');
  $page = $this->uri->segment(3);
  $results = $this->my_model->search($q, $page);
  $this->load->view('search_tpl', $results);
}

search_tpl.php

echo form_open(current_url(), array('method' => 'get'));
echo form_input('q');
echo form_submit('search', 'Search');
echo form_close();

您可以在分頁$ config變量中使用page_query_string

$config['page_query_string'] = TRUE;

啟用此選項后,$ _ GET將用於創建分頁鏈接。

結果 example/all/?per_page=20 example/all/?per_page=20

你只要改變

$config["uri_segment"] = 3; $config["uri_segment"] = 4;

並像這樣更改base_url

$config["base_url"] = "site_url('student/index')".$search_key;

並獲得search_key

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM