In truth, providing the previous “Blogger” controller with the capacity for paginating blog entries is only a matter of using the pagination class that comes bundled with Code Igniter. It’s that simple, believe me. However, it’s necessary to demonstrate how to perform this task with some functional code. Therefore, below I included the improved signature of the controller class, which this time will be capable of paginating all of the blog entries. Have a look at it, please: class Blogger extends Controller{ function Blogger(){ // load controller parent parent::Controller(); // load database class and connect to MySQL $this->load->database(); // load pagination library $this->load->library('pagination'); // load helper $this->load->helper('url'); } // display all blog entries function blogs(){ $data['title']='Blog Entries Listing'; $data['result']=$this->db->get('blogs',3,$this->uri->segment(3)); // set pagination parameters $config['base_url']='http://127.0.0.1/codeigniter/index.php/blogger/blogs/'; $config['total_rows']=$this->db->count_all('blogs'); $config['per_page']='3'; $config['full_tag_open']='<div id="paginglinks">'; $config['full_tag_close']='</div>'; $this->pagination->initialize($config); // create pagination links $data['links']=$this->pagination->create_links(); // load 'blogger_view' view $this->load->view('blogs_view',$data); } } As shown above, the controller class now incorporates a few interesting changes that deserve a closer analysis. First, apart from loading the database class and the “url” helper, it includes the pagination class, which not surprisingly is called “pagination.” So far, the way that the constructor works is very understandable, isn’t it? Thus, move on and take a close look at the “blogs()” method. As you can see, it fetches chunks of rows from the “blogs” MySQL table, and also assigns some settings to the pagination class, such as the base URL and the number of rows that will be displayed per page, the opening and close tags that will be used for showing the paging links, and so forth. Once this class have been configured correctly, the blog entries are embedded into the respective view, along with the paginated links in question, to be displayed later on the browser. Simple and sweet! At this point, the controller class has the ability to span the blog entries across several pages. Of course, there are other parameters that you can use to configure the pagination class, but for the moment I’ll use the ones discussed before. Now that the controller class has been enhanced, it should be saved to the Code Igniter /system/application/controllers/ folder as “blogger.php” for later use. Did you do it? Then it’s time to see how to modify the view file, so it can display the paginated links as well. Precisely, this will be the subject that I’m going to discuss in the next section, so go ahead and read the next segment.
blog comments powered by Disqus |
|
|
|
|
|
|
|