• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pagination Detection

Has any found a way to detect whether there is a Pagination Object present from the Controller?

I have a situation where I need to check for the existence of pagination from the controller. I am checking for the proper uri segment, but that doesn't help for the first page (which has no segment but is a pagination set).

I did a print_r() on the CI object and could not find anything resembling pagination.

I hope someone knows of something simple I am overlooking.

Thanks much! :-)

Let's say your url looks like this: "http://www.example.com/mycontroller/myfunction/3" where "3" is supposed to be the page.

Then your controller would look like this:
class Mycontroller extends Controller
    function Mycontroller

    function myfunction ($page = 1)
        echo $page;
and the output would be "3".

If you go to "http://www.example.com/mycontroller/myfunction" (notice the missing page parameter), the output would be "1".

There is no need to look for a pagination object (whatever that would be). Maybe you are trying to find out if the pagination library is loaded?


I am trying to find out if the pagination library is loaded.

Here's why.... (and maybe I have this all wrong)

I don't normally pass variables around like your example illustrates (and maybe I should, I don't know). Instead, since there is a uri segment available to each part of the application, I base my logic on results from testing the uri segments.

HOWEVER, now that I have built a good portion of this project, I am needing to see if the current page is using pagination even if it is page 1

I normally do checks like:

  // do stuff

Obviously, I might need to re-assess this structuring for the future, but now I need to jump this hurdle to move forward on this project.

Thanks for the input.

Any comments are appreciated.

Well, even though I would still like to get some input on this. I found another way to do my particular check.

I did learn in the process though that there are 2 libraries that cannot be extended:

Database and Loader

This was a result of my trying to extend the Loader class to enable the CI Singleton to detect if it was loaded (The CI object knows about some libraries but not all).

So I had to find another way.

Thanks though.

[eluser]Craig A Rodway[/eluser]
In the controller where I use pagination, the index() function is the main listing page, and the p() function handles the pages. If you check the contents of $offset, you can determine if you are on the index page or on a paginated page. Hope this code helps...

function index(){
        return $this->p();

function p(){
        $limit = 5;
        $offset = $this->uri->segment(3, 0);
        $config['per_page'] = $limit;
        $config['base_url'] = site_url('news/p');
        $config['total_rows'] = $this->newsmodel->count();    
        $body['news'] = $this->newsmodel->get(NULL, NULL, TRUE, $limit, $offset);
        $tpl['title'] = 'News';
        $tpl['body'] = $this->load->view('news/index', $body, TRUE);
        $tpl['sidebar_activities'] = $this->activitymodel->get_sidebar();
        $this->load->view(TEMPLATE, $tpl);

/news/ = index()
/news/p/5 = p()

Well, there is another way but it only sees if the lib is loaded. (which means, if loaded by autoload or so, it's also present.)
Since all loaded classes are added to the main CI object ($this, in your conrollers, views, models) you could check it for presence of the pagination lib, but ONLY if you only load that lib when you need it.

try to call something like
. I'm not sure if that's the right way, since I didn't test it, and if it don't work you can always do this trick:

echo "<pre>";
echo "<pre>";

Now, you are able to to see the entire CI object, and find out wheter to use isset, empty(), or check for true or false.
Note: be careful when using that, it also dumps your entire config INCLUDING encryption keys and stuff. so... try it locally, not 'live'.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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