Site search with CodeIgniter?

你说的曾经没有我的故事 提交于 2019-12-12 09:16:20

问题


I need to make a simple site search with pagination in it; could anyone tell me how to do it without affecting the URL structure? Currently I'm using the default CodeIgniter URL structure and I have removed index.php from it. Any suggestions?


回答1:


You could just use a url like /search/search_term/page_number.

Set your route like this:

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

And your controller like this:

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
}



回答2:


Just to update this question. It is probably best to use the following function:

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

and the result will then put everything into an associative array like so:

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

Read more about the URI Class at CodeIgniter.com




回答3:


Don't quite understand what you mean by "affecting the url structure". Do you mean you'd want pagination to occur without the URL changing at all?

The standard pagination class in CI would allow you to setup pagination so that the only change in the URL would be a number on the end

e.g if you had 5 results to a page your urls might be

http://www.example.com/searchresults

and then page 2 would be

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

and page 3 would be

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

and so on.

If you wanted to do it without any change to the URL then use ajax I guess.




回答4:


Code Igniter disables GET queries by default, but you can build an alternative if you want the url to show the search string.

Your url can be in the notation 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 ; 
} 

source: http://codeigniter.com/wiki/alternative_to_GET/



来源:https://stackoverflow.com/questions/2817864/site-search-with-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!