Take the 2-minute tour ×

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?

share|improve this question

4 Answers 4

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
share|improve this answer

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:

    'name' => 'joe'
    'location'  => 'UK'
    'gender'    => 'male'

Read more about the URI Class at CodeIgniter.com

share|improve this answer

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


and then page 2 would be


and page 3 would be


and so on.

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

share|improve this answer
yes, but how would i pass the search term to next page??? –  sonill May 16 '10 at 6:06
You could use a hidden field –  Kenzo Apr 2 '13 at 6:08
or use flashdata –  bradfields Apr 2 '13 at 15:09

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/

share|improve this answer

Your Answer


