Welcome Guest, Not a member yet? Register   Sign In
Cannot extend pagination class?
#1

[eluser]fancms[/eluser]
Because there are several config settings for pagination that I would use over and over again it made sense (to me) to create a function where, when using it, I could simply plug in the 2 or 3 variables needed and all would be well.

However, when I tried to create a library extending the Pagination class (which I assume is what I would need to do to extend said class), I keep getting a Fatal error that says the pagination class does not exist. I've tried several different methods, including loading the Pagination class prior to using my library, and still no go. I double-checked that the library I wrote was in the /application/libraries folder. I'm simply stumped.

Here's the code I have for my library:

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Quickpage extends Pagination {
    
function Quickpage() {
     parent::Pagination();
    
}
    
function go($pageit,$totalrows,$perpage)
     {
          $domain = $this->config->item('base_url');
          $config = array("base_url" => $domain.$pageit,
                          "total_rows" => $totalrows,
                          "per_page" => $perpage,
          );
          
          $this->pagination->initialize($config);

          return $this->pagination->create_links();
     }    
    
}
?>

The pagination function works fine by itself, so I'm totally confused here 8-/
#2

[eluser]xwero[/eluser]
it's CI_Pagination, most libraries are prefixed with CI_
#3

[eluser]esra[/eluser]
The subclass_prefix variable in config.php is the subclass prefix that CodeIgniter checks for in application/libraries. The default setting is MY_. If a file called MY_Pagination.php exists in application/libraries/ and the class name is MY_Pagination extended from CI_Pagination, it will be automatically loaded as an extension to the CI_Pagination class.

You can change MY_ to anything you want. For example, you could change 'MY_' to 'C' and create a CPagination class.

----
EDIT: I have not tried this in a while, but you could. If a file called Pagination.php exists in application/libraries/, CI should load it as a complete replcement for sytem/libraries/pagination.php.

The CI_ and MY_ solutions are used to work around PHP's lack of support for namespaces. If instead you wanted to add support or namespaces to CI, you might be able to do it using the namespaces library from the Claw framework's core (core.namespaces), or one of the solutions posted elsewhere on the web (e.g., phpclasses.org).
#4

[eluser]fancms[/eluser]
I have tried both ways - with the MY_ and CI_ and without and I get the same message 8-/
#5

[eluser]xwero[/eluser]
[quote author="fancms" date="1193595154"]

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Quickpage extends Pagination {
    
function Quickpage() {
     parent::Pagination();
    
}
    
function go($pageit,$totalrows,$perpage)
     {
          $domain = $this->config->item('base_url');
          $config = array("base_url" => $domain.$pageit,
                          "total_rows" => $totalrows,
                          "per_page" => $perpage,
          );
          
          $this->pagination->initialize($config);

          return $this->pagination->create_links();
     }    
    
}
?>
[/quote]
change it to
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Quickpage extends CI_Pagination {
    
function Quickpage() {
     parent::CI_Pagination();
    
}
    
function go($pageit,$totalrows,$perpage)
     {
          $domain = $this->config->item('base_url');
          $config = array("base_url" => $domain.$pageit,
                          "total_rows" => $totalrows,
                          "per_page" => $perpage,
          );
          
          $this->initialize($config);

          return $this->create_links();
     }    
    
}
?>
You don't have to reference to an instance of the pagination class because you are extending it. All the CI methods act like they are a part of the extended class, that's the magic of inheritance.
#6

[eluser]fancms[/eluser]
Ahh, I see. Thanks, xwero; it's working perfectly now Smile
#7

[eluser]paulopmx[/eluser]
Ahem. :-)

Why don't you try my function at http://ellislab.com/forums/viewthread/64287/

It does the same thing, but you are only given the values you need to construct your own custom paging style in the view files. So you are no longer stuck with just link style paging, and because its just a function you add to a plugin no more init configs.




Theme © iAndrew 2016 - Forum software by © MyBB