• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
search feature & result with pagination links


I am trying to add a 'search' feature to my site. By using the 'pagination' class of CI, I could able to display the 'pagination links'. But when I click on the second or any other link, it doesn't work like what I expected. It is because, I couldn't pass the searched word to next page. The pagination class returned the pagination links and it doesn't contained the 'search keyword'.

How can I create a Good Search feature in CI & user can navigate through the displayed list through pagination?

Thanks & Regards,
PHP Developer.

[quote author="Aniesh" date="1220377399"]I couldn’t pass the searched word to next page[/quote]
Try to use session_flash_data to pass your keyword...or you can use your keyword to be part of URI

another way can be pass the search keyword(or encrypted keyword) by another segment. like

When passing the search criteria in the URL, you have to be careful because the user can type in ANYTHING. However, you have a few options:

1) enable query strings
2) allow additional characters in the URL using the config setting
3) encode the search criteria in some way

I chose #3 because I didn't want to open my app to security risks. You'll be giving up human-readable URLs, but in my opinion, security is more important.

I used a modified base64_encode/decode for my search strings. The only problem with the normal base64_encode function is that it can introduce some additional chars that are not allowed by default in CI, so I extended the URL helper with these functions in one of the comments from the base64_encode documentation on php.net:

function base64_url_encode($input) {
    return strtr(base64_encode($input), '+/=', '-_,');

function base64_url_decode($input) {
    return base64_decode(strtr($input, '-_,', '+/='));

Hope this helps.

Thank you for all of your responses.

Actually, I couldn't make any changes in the code after I read your suggestions.. So I would like to post the code here for making more clear:

The search form is:

            <TD align="center">&nbsp;</TD>
            <TD>Search Feeds:&nbsp;&lt;input type="text" name="feed" value="" /&gt; </TD>

            &lt;input type="hidden" name="search_view_form" value="1" /&gt;
            &lt;input type="submit" value="Submit" /&gt;

Search Controller is


class Search extends Controller

    function __construct()


        $this->load->library(array('session', 'pagination', 'validation'));

        $this->load->helper(array('form', 'url'));



    function index()

        * Feed Searching:
        * Doesn't receive a keyword, then no Feed will be displayed.
        * if receive a keyword, then it stored into the 'Session'.
        * Use this Session value in next searching case, when the user click on the Pagination Links.

        $data['feeds']         =     array();
        $data['pagination_links']=    '';

        $search_view_form    =    $this->input->post('search_view_form');

        $keyword         =     $this->input->post('feed');

        if($search_view_form == 1 AND $keyword != "")
            $this->session->set_userdata('search_keyword_feed', $keyword);
        elseif($search_view_form == 1 AND $keyword == "")

            $keyword     =     $this->session->userdata('search_keyword_feed');
            $config['base_url']     =     site_url().'/search/index/';    
            // Calling the Model function for getting the total number of records.
            $config['total_rows']     =     $this->Search_model->search_num_rows($keyword);
            // Number of Records displayed per page.
            $config['per_page']     =     '2';
            // calling Model for getting the data.
            $data['feeds']         =     $this->Search_model->search_feed($keyword, $config['per_page'], $this->uri->segment(3,0));
            $data['pagination_links']=    $this->pagination->create_links();
        // load the view



When I make a new search, then the form submits to a URL like:


When I click on the 2nd link from the pagination links, then the URL:


And when I click on the 1st link to go back to the first page of the search result, then the URL is:


My requirement is to pass the 'Search Keyword' along with the URL itself. Presently I stored this search keyword inside the Session & I don't that its good practise!

I would like to hear from you all..

Thank You,
php developer.

Hi Aniesh,

I had the same problem a time ago and can recommend
you to read this post:

There you can find different approaches to solve your problem.

Hello Mr.LuckyFella73,

Thank you for your reply.

I gone through allmost all the responses one by one. Many suggestions but after some time, they all end with bug related to that suggestion..

At the end of the discussion, it ends with what I posted here. My suggestion was:

1. Create a search form and also add one hidden field.

2. Store the search keyword in the Session, if the hidden variable has value.

3. Otherwise, the page gets controll from pagination and we want to show the requested page using the keyword that is stored in the Session.

thats all!!

I just started to work with CI from last two weeks.. I hope that I can post a good method soon here.

Thank You All.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.