CodeIgniter simple CRUD improved

Currently I’m developing a web application in PHP using the CodeIgniter framework. Since I needed some admin functionality such as create, read, update and delete records (CRUD) I searched for CodeIgniter libraries. I came across this post by Henri. His solution is very simple and easy to understand and implement. In this post I’ll describe how to improve his solution so it’s possible to sort columns. I start with the files Henri provided in his post.

In order to enable sorting I first modify the get_paged_list function of the PersonModel class:

The index function of the person controller must be modified as well. I changed the following parts:

  • Function parameters
  • Get ordering from URI
  • Call to model
  • Table header

Updated June 29th 2010 thanks to Stephen.
The only problem now is pagination doesn’t work any more. As far as I know there is no solution to pass the ordering parameter to CodeIgniter’s pagination class. I think the best solution is to implement your own pagination class. Maybe I’ll discuss that in another post.

4 thoughts on “CodeIgniter simple CRUD improved

  1. Also, I should note that your sort URIs should be formatted as follows (under CodeIgniter convention):

    This allows the function to automatically receive the arguments passed in the URI. You should of course initialize their values to defaults if they are empty.

  2. The index function is a controller action indeed, it’s the same index function as in Henri’s version. And thanks for your uri segment tip, I didn’t know that!

Leave a Reply

Your email address will not be published. Required fields are marked *