2010-05-12 41 views
1

我需要做一個簡單的網站搜索與分頁;誰能告訴我如何做到這一點,而不會影響網址結構?目前我使用的是默認的CodeIgniter URL結構,我已經從中刪除index.php。有什麼建議麼?使用CodeIgniter進行網站搜索?

回答

0

不太明白你的意思是「影響網址結構」。你的意思是你希望分頁在沒有URL改變的情況下發生?

在CI標準分頁類允許您設置分頁,以便在URL中唯一的變化將是對結束

例如,如果你有5個結果頁面的數量你的URL可能是

http://www.example.com/searchresults

,然後第2頁將

http://www.example.com/searchresults/5

和第3頁將

http://www.example.com/searchresults/10

等。

如果你想這樣做沒有任何改變的網址,然後使用阿賈克斯我猜。

+0

是的,但如何我會將搜索詞傳遞給下一頁嗎? – sonill 2010-05-16 06:06:46

+0

您可以使用隱藏字段 – Kenzo 2013-04-02 06:08:38

+0

或使用flashdata – bradfields 2013-04-02 15:09:38

3

您可以使用像/search/search_term/page_number這樣的網址。

設置你的路線是這樣的:

$route['search/:any'] = "search/index"; 

而且您的控制器是這樣的:

function index() 
{ 
    $search_term = $this->uri->rsegment(3); 

    $page = (! $this->uri->rsegment(4)) ? 1 : $this->uri->rsegment(4); 

    // some VALIDATION and then do your search 
} 
0

代碼點火器禁用GET默認查詢,但你可以建立一個替代如果你想顯示搜索字符串的網址。

您的網址,可在符號 www.yoursite.com/index.php/class/function/request1:value1/request2:value2

$request = getRequests(); 
echo $request['request1']; 
echo $request['request2']; 

function getRequests() 
{ 
    //get the default object 
    $CI =& get_instance(); 
    //declare an array of request and add add basic page info 
    $requestArray = array(); 
    $requests = $CI->uri->segment_array(); 
    foreach ($requests as $request) 
    { 
     $pos = strrpos($request, ':'); 
     if($pos >0) 
     { 
      list($key,$value)=explode(':', $request); 
      if(!empty($value) || $value='') $requestArray[$key]=$value; 
     } 
    } 
    return $requestArray ; 
} 

來源:http://codeigniter.com/wiki/alternative_to_GET/

1

剛更新這個問題。這可能是最好使用下列功能:

$uri = $this->uri->uri_to_assoc() 

,然後將結果就會把一切都變成一個關聯數組,像這樣:

[array] 
(
    'name' => 'joe' 
    'location' => 'UK' 
    'gender' => 'male' 
) 

瞭解更多關於URI Class at CodeIgniter.com