Welcome Guest, Not a member yet? Register   Sign In
Pagination Detection
#1

[eluser]wowdezign[/eluser]
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! :-)
#2

[eluser]davidbehler[/eluser]
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:
Code:
class Mycontroller extends Controller
{
    function Mycontroller
    {
        parent::controller();
    }

    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?
#3

[eluser]wowdezign[/eluser]
Exactly.

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:

Code:
if(!$this->uri->segment(3)){
  redirect('some_page');
}
if($this->uri->segment(3)){
  // do stuff
}else...

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

[eluser]wowdezign[/eluser]
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.
#5

[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...

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

Code:
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();    
        $this->pagination->initialize($config);
        
        $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()
#6

[eluser]Fabdrol[/eluser]
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
Code:
if(isset($this->pagination))
. 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:

Code:
echo "<pre>";
var_dump($this);
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'.




Theme © iAndrew 2016 - Forum software by © MyBB