Provided that you already grasped how the previous controller class paginates all of the blog entries via the corresponding pagination class, now it’s time to modify the signature of the view file so that it can display the pertinent paginated links too. As you saw before, the controller passes to the view a $data array that contains an element referenced as $data[‘links’]. This one is actually the string that renders the paginated links, so in order to display them along with the blog entries, the view should be modified in the following way: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title><?php echo $title;?></title> </head> <body> <h1><?php echo $title;?></h1> <?php foreach($result->result_array() as $blog):?> <h2><?php echo $blog['title'];?></h2> <p><?php echo $blog['text'];?></p> <p><?php echo anchor('blogger/comments/'.$blog['id'],'View Blog Comments >>');?></p> <?php endforeach;?> <?php echo $links;?> </body> </html> Now, all of the blog entries fetched from the “blogs” MySQL table will be displayed across several pages, along with the paginated links. In addition, you should first save the improved version of this view to the Code Igniter /system/application/views/ folder as “blogs_view.php.” Once you’ve done this, type the following URL into your browser’s address field: http://localhost/codeigniter/index.php/blogger/blogs/ If all goes as expected, you should get the below output:
Here you have it! At this point you should feel pretty satisfied, since now the blog application is capable of displaying a group of blog entries which have been paginated. Definitely, that was a neat improvement, right? To wrap up this tutorial, I encourage you to tweak all of the code samples I've included here, so you can introduce more improvements to the blog application. Final thoughts In this second chapter of the series, I demonstrated how to add record pagination capabilities to the blog application built earlier. As you saw for yourself, this process only required using the Code Igniter’s pagination class, and introducing a few simple changes to the controller and the view as well. In the upcoming article, I’ll continue adding more features to the blogger, such as the ability to post comments on each blog entry. However, to learn how this will be done, you’ll have to read the next part! See you next week.
blog comments powered by Disqus |
|
|
|
|
|
|
|