Welcome Guest, Not a member yet? Register   Sign In
search feature & result with pagination links
#1

[eluser]Joseph1982[/eluser]
Hello,

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.
#2

[eluser]anggie[/eluser]
[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
#3

[eluser]Sumon[/eluser]
another way can be pass the search keyword(or encrypted keyword) by another segment. like
www.mysite.com/search/pageno/keyword
#4

[eluser]lukeinjax[/eluser]
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:

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

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

Hope this helps.
#5

[eluser]Joseph1982[/eluser]
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:

Code:
<?=form_open('search')?>
    <table>
        <TR>
            <TD align="center">&nbsp;</TD>
        </TR>
        <TR>
            <TD>Search Feeds:&nbsp;&lt;input type="text" name="feed" value="" /&gt; </TD>
        </TR>

        <TR>
            <TD>
            &lt;input type="hidden" name="search_view_form" value="1" /&gt;
            &lt;input type="submit" value="Submit" /&gt;
            </TD>
        </TR>
    </table>
&lt;/form&gt;


Search Controller is


Code:
&lt;?php

class Search extends Controller
{

    function __construct()
    {

        parent::Controller();

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

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

        $this->load->model('Search_model');

    }

    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 == "")
        {
            $this->session->unset_userdata('search_keyword_feed');
        }

        if($this->session->userdata('search_keyword_feed'))
        {
            $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';
            
            $this->pagination->initialize($config);
            
            // 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
        $this->load->view('search_results_view',$data);
    }

}

?&gt;





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

http://mysite.com/index.php/search


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

http://mysite.com/index.php/search/index/2


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

http://mysite.com/index.php/search/index/


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.
#6

[eluser]LuckyFella73[/eluser]
Hi Aniesh,

I had the same problem a time ago and can recommend
you to read this post:
http://ellislab.com/forums/viewthread/44845/

There you can find different approaches to solve your problem.
#7

[eluser]Joseph1982[/eluser]
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.




Theme © iAndrew 2016 - Forum software by © MyBB