Welcome Guest, Not a member yet? Register   Sign In
Pagination - Not working... again!
#1

[eluser]Devon Lambert[/eluser]
Ok,

I have no idea how I did it, but I've managed to break the pagination class, yet again.

I've been at this for about 1.5 hours now and I think it's time for bed. Before I retire, I wanted to submit this to the community and see if anyone may be able to assist?

I am able to get the first search page to show in the pagination but after that, my keyword/post data is lost, even though it should be getting pulled from the uri.

Here is the Search Controller:

Code:
<?php

class search extends Controller {



    function search()

    {

        parent::Controller();

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



    }

    

    function index()

    {

        $data['title'] = "MY SEARCH FUNCTION";

        if ($this->input->post('keyword') != '') {

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

        }

        else {$keyword = $this->uri->segment(2);}

        

        

        $data['count_results'] = $this->mdl_search->countResults($keyword);

            

        $this->load->library('pagination');



        $config['base_url'] = site_url('search/' . url_title($keyword));

        $config['total_rows'] = $data['count_results'];

        $config['uri_segment'] = '3';

        $config['per_page'] = '10';



        $this->pagination->initialize($config);



        $offset = $this->uri->segment(3);

        

        $data['search_results'] = $this->mdl_search->getSearchResults($keyword, $config['per_page'], $offset);

                        

        $this->load->view('arcade/header');

        $this->load->view('arcade/search', $data);

        $this->load->view('arcade/footer');

    }



}

?>

Here is the view:

Code:
<div class="search">

        &lt;?php if(isset($search_results) && !empty($search_results)) { ?&gt;

            <h2>&lt;?= $title;?&gt;</h2>

            <div class="pagination">&lt;?php echo $this->pagination->create_links(); ?&gt;</div>

            <table class="search-results">

                    <tr class="headerRow">

                        <th class="gameTitle"><a href="#"><span>Game Title</span></a></th>

                        <th class="active"><a href="#"><span>Players</span></a></th>

                        <th><a href="#"><span>Rating</span></a></th>

                        <th><a href="#"><span>Reviews</span></a></th>

                        <th><a href="#"><span>Play Type</span></a></th>

                    </tr>

                    

                        &lt;? foreach ($search_results as $search_result): ?&gt;

                            <tr class="alt">

                                <td class="title"><a href="#"><img src="&lt;?php echo base_url() ?&gt;images/img/&lt;?php echo $search_result['nameid'];?&gt;.png" alt="&lt;?php echo $search_result['name'];?&gt;" width="60" height="50"></a><div class="fltLft"><a href="#">&lt;?php echo $search_result['name'];?&gt;</a><br />&lt;?php echo $search_result['desc'];?&gt;</div></td>

                                <td class="players">1,262</td>

                                <td class="ratings"><b>5.00</b><br />out of 5.0</td>

                                <td class="reviews"><span class="up">72%</span><br />491 reviews</td>

                                <td><a href="#" class="btnSearchPlayNow clearLink">play now</a><a href="/servlet/DownloadEcommTracker?sku=texttwist&promoCode=SearchList" class="#">download</a></td>

                            </tr>

                        &lt;? endforeach; ?&gt;

            </table>

        &lt;?php } else { ?&gt;

            <h3>You didn't enter a search result, please re-renter.</h3>

        &lt;?php } ?&gt;

    </div>

Thanks to any who may help. :-)
#2

[eluser]Bramme[/eluser]
Dunno if this is the solution, but I would restructure the way you're approaching your $keyword handling. I would go about it this way:

controller:
Code:
&lt;?php

class search extends Controller {

    function search()
    {

        parent::Controller();

        $this->load->model('mdl_search');
        $this->load->helper('url');
        
        $data['title'] = "MY SEARCH FUNCTION";
        $this->load->vars($data);

    }

    function index()
    {

        if($this->input->post('submit') != false)
        {
            $keyword = $this->input->post('keyword', true); //parses it through the XSS filter
            redirect("/search/keyword/$keyword");
        }
        
        $this->load->view('searchform');
    }
    
    function keyword()
    {
        
        $keyword = $this->uri->segment(3, null);
        
        if(!isset($keyword))
        {
            redirect('/search'); // extra precaution
        }
        
        $data['count_results'] = $this->mdl_search->countResults($keyword);

        $this->load->library('pagination');

        $config['base_url'] = site_url('search/keyword/' . url_title($keyword));
        $config['total_rows'] = $data['count_results'];
        $config['uri_segment'] = '4';
        $config['per_page'] = '10';

        $this->pagination->initialize($config);

        $offset = $this->uri->segment(4, 0);

        $data['search_results'] = $this->mdl_search->getSearchResults($keyword, $config['per_page'], $offset);

        $this->load->view('arcade/header');

        $this->load->view('arcade/search', $data);

        $this->load->view('arcade/footer');

    }



}

?&gt;
searchform view:
Code:
&lt;form action="/search/" method="post"&gt;
    &lt;input type="text" name="keyword" /&gt;
    &lt;input type="submit" name="submit" value="Submit search" /&gt;
&lt;/form&gt;
and your final view
Code:
<div class="search">

        <h2>&lt;?= $title;?&gt;</h2>

            <div class="pagination">&lt;?php echo $this->pagination->create_links(); ?&gt;</div>

            <table class="search-results">

                    <tr class="headerRow">

                        <th class="gameTitle"><a href="#"><span>Game Title</span></a></th>

                        <th class="active"><a href="#"><span>Players</span></a></th>

                        <th><a href="#"><span>Rating</span></a></th>

                        <th><a href="#"><span>Reviews</span></a></th>

                        <th><a href="#"><span>Play Type</span></a></th>

                    </tr>

                    

                        &lt;? foreach ($search_results as $search_result): ?&gt;

                            <tr class="alt">

                                <td class="title"><a href="#"><img src="&lt;?php echo base_url() ?&gt;images/img/&lt;?php echo $search_result['nameid'];?&gt;.png" alt="&lt;?php echo $search_result['name'];?&gt;" width="60" height="50"></a><div class="fltLft"><a href="#">&lt;?php echo $search_result['name'];?&gt;</a><br />&lt;?php echo $search_result['desc'];?&gt;</div></td>

                                <td class="players">1,262</td>

                                <td class="ratings"><b>5.00</b><br />out of 5.0</td>

                                <td class="reviews"><span class="up">72%</span><br />491 reviews</td>

                                <td><a href="#" class="btnSearchPlayNow clearLink">play now</a><a href="/servlet/DownloadEcommTracker?sku=texttwist&promoCode=SearchList" class="#">download</a></td>

                            </tr>

                        &lt;? endforeach; ?&gt;

            </table>

    </div>
(now there's no if)

Try this. I hope it works, didn't test it.

note: you should also handle "no search results"
#3

[eluser]Devon Lambert[/eluser]
Thanks Bramme,

Tried it and it doesn't seem to be working properly.

I'll have dig in deeper tonight.

It's still dropping the post data. :-(
#4

[eluser]Bramme[/eluser]
what do you mean with post data?
#5

[eluser]Devon Lambert[/eluser]
[quote author="Bramme" date="1216240513"]what do you mean with post data?[/quote]

Sorry,

I meant that for some reason the search function does not receive any post data once I click the 2nd paginated link.

Thus I am assuming that some how the keyword has been lost in the process?

Thus, it shows the error message stating that no search term was entered.

Thanks again for your assistance. The CI community is ever willing. :-)
#6

[eluser]Devon Lambert[/eluser]
I have stepped back to an older version of the code when the search/pagination was still working. I will try this route again and see what happens.

I think it had something to do with dropping all of my controllers into a sub folder and trying to solve all my problems with routes.




Theme © iAndrew 2016 - Forum software by © MyBB