I've often see forum posts asking why the Pagination class isn't working. I decided to create a tutorial on the Pagination class for those who need to learn about the pagination class.
One important thing you need to learn is that the Pagination class doesn't do the querying for you. It doesn't 'connect' to the database like most pagination classes.
The Pagination class ONLY creates the links for navigation.
Let's now start with the pagination tutorial. This tutorial assumes that you have connected your CodeIgniter application to the database.
First, we need to create a table.
The method
The method
Next on our list is creating the controller function.
We should consider how, CodeIgniter generates URLs. For example, our sample creates the following URL if you haven't applied any routing configuration.
The very first part of the method
The first statement loads the model, posts_model. It is required if you will be accessing any of its methods. The
Next stop is the pagination config.
The next thing we need to do is to pass the 'limited' number of posts to the 'View' like so.
This can be easily modified to other setups when routing, etc.
Categories: Web Development
One important thing you need to learn is that the Pagination class doesn't do the querying for you. It doesn't 'connect' to the database like most pagination classes.
The Pagination class ONLY creates the links for navigation.
Let's now start with the pagination tutorial. This tutorial assumes that you have connected your CodeIgniter application to the database.
First, we need to create a table.
CREATE TABLE IF NOT EXISTS `articles` ( `id` int(11) NOT NULL auto_increment, `title` varchar(50) NOT NULL, `content` text NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Next, we create our own Model for our articles table.<?php class Articles_model extends Model { function Articles_model() { parent::Model(); } function get_posts($limit = NULL, $offset = NULL) { $this->db->limit($limit, $offset); return $this->db->get('posts'); } function count_posts() { return $this->db->count_all_results('posts'); }
The method Articles_model()
is the constructor. In PHP 4, constructors are named after the class they are located in.The method
get_posts()
, retrieves the information from the database. Notice that it has parameters for limiting and the offset of returned rows.The method
count_posts()
, retrieves the total number of posts available in the table.Next on our list is creating the controller function.
<?php class Posts extends Controller { function Posts() { parent::Controller(); } function manage() { $this->load->model('posts_model'); $per_page = 10; $total = $this->posts_model->count_posts(); $data['posts'] = $this->posts_model->get_posts($per_page,
$this->uri->segment(3)); $base_url = site_url('posts/manage'); $config['base_url'] = $base_url; $config['total_rows'] = $total; $config['per_page'] = $per_page; $config['uri_segment'] = '3'; $this->pagination->initialize($config); $this->load->view('admin/posts/manage', $data); }
Copy and paste and do not worry if you don't understand that chunk of code yet. We will get back to that later on.We should consider how, CodeIgniter generates URLs. For example, our sample creates the following URL if you haven't applied any routing configuration.
http://www.yoursite.com/index.php/post/manage
If you knew that, very good because it will be needed in our pagination tutorial and whenever you use the pagination class.The very first part of the method
manage()
are as follows:$this->load->model('posts_model'); $per_page = 10; $total = $this->posts_model->count_posts(); $data['posts'] = $this->posts_model->get_posts($per_page, (int)
$this->uri->segment(3));
The first statement loads the model, posts_model. It is required if you will be accessing any of its methods. The
$per_page
variable is the number of posts we want to retrieve on every page. The variable, $total gets the total number of posts in the table as seen in the posts_model method, count_posts()
.$data['posts']
is a bit more complex. Remember that with this method, we will generation this:http://www.yoursite.com/post/manage
$this->uri->segment(3)
would get the value of the uri segment after the segment, 'manage'. Right now, there is nothing. So we get 0 (zero) which will mean that we will start the offset on 0 (zero) and retrieve 10 rows.Next stop is the pagination config.
$base_url = site_url('posts/manage'); $config['base_url'] = $base_url; $config['total_rows'] = $total;
$config['per_page'] = $per_page; $config['uri_segment'] = '3'; $this->pagination->initialize($config);
The $base_url
will be the 'base' url that will be used for generating pagination links. With this $base_url value we would get:http://www.yoursite.com/post/manageWhen we click on the 'next' link on the CodeIgniter Pagination links, we will get:
http://www.yoursite.com/post/manage/10Now, we can easily know what the SQL will be. Remember that we will be retrieving the posts with offset equals the value after the segment, 'manage' and right now we have 10. Now we will be retrieving 10 records with the offset of 10.
The next thing we need to do is to pass the 'limited' number of posts to the 'View' like so.
$this->load->view('admin/posts/manage', $data);
In the View, loop over the results:foreach ($posts->results() as $post): echo $post->title, '<br />'; echo $post->content; endforeach;
This would effectively loop through the records to be outputted to the browser. The final part is creating the Pagination links by typing this in the View:<?php echo $this->pagination->create_links(); ?>
We are done!This can be easily modified to other setups when routing, etc.
Categories: Web Development
No comments:
Post a Comment