Indeed, the philosophy that drives Code Igniter allows us to simplify the development of complex applications. And paginating database records certainly isn’t an exception, since the framework includes a solid pager class, which can be easily customized to suit the requirement of a huge number of projects. In this particular case, where it’s necessary to create a user controller class that uses the methods of the model coded previously, the pager class of Code Igniter fits perfectly into this schema. Below I built a brand new controller that exploits the functionality of this pager class to paginate rows fetched from the prior “users” MySQL table. Here’s the code for this class: class Users extends Controller{ function Users(){ // load controller parent parent::Controller(); // load 'Users' model $this->load->model('Users'); } function display($row=0){ // load pagination library $this->load->library('pagination'); // set pagination parameters $config['base_url']='http://127.0.0.1/codeigniter/index.php/users/display/'; $config['total_rows']=$this->Users->getNumUsers(); $config['per_page']='5'; $this->pagination->initialize($config); // store data for being displayed on view file $data['users']=$this->Users->getUsers($row); $data['title']='Displaying user data'; $data['header']='User List'; $data['links']=$this->pagination->create_links(); // load 'testview' view $this->load->view('users_view',$data); } } As illustrated above, the “User” controller class looks pretty similar to the one built in the previous tutorial. However, there's an important difference that I’d like to point out here, since it implements a new method called “display().” As you can see, the method in question first includes, via the corresponding loader object, the pagination class. Then, it initializes some of its parameters, such as the base URL of the paging links, the total number of rows that will be spawned, and finally the number of records that will be displayed per page. Once the pagination class has been correctly set up, it’s used for creating the paging links via its “create_links()” method. Lastly, the “display()” method retrieves the paginated rows from the corresponding “users” MySQL table, which are directly embedded along with the pertinent links into a view file. And before I forget, please save the user controller class to the Code Igniter system/application/controllers/ folder. However, if you’re anything like me, you wonder how the view file will be created, right? Thus, in the last section of this tutorial I’m going to show you how to define this file, in this way finishing the development of this MySQL-driven application aimed at demonstrating how to paginate database rows with Code Igniter. What are you waiting for? Jump forward and read the next section!
blog comments powered by Disqus |
|
|
|
|
|
|
|