marthijn. Rotating Header Image

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:

function get_paged_list($limit = 10, $offset = 0, $order_column = '', $order_type = 'asc'){
	if (empty($order_column) || empty($order_type))
		$this->db->order_by('id','asc');
	else
		$this->db->order_by($order_column, $order_type);
	return $this->db->get($this->tbl_person, $limit, $offset);
}

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
function index($offset = 0, $order_column = 'id', $order_type = 'asc'){
	// checks
	if (empty($offset)) $offset = 0;
	if (empty($order_column)) $order_column = 'id';
	if (empty($order_type)) $order_type = 'asc';
	//TODO: check for valid column
 
	// load data
	$persons = $this->personModel->get_paged_list($this->limit, $offset, $order_column, $order_type)->result();
 
	// generate pagination
	$this->load->library('pagination');
	$config['base_url'] = site_url('person/index/');
	$config['total_rows'] = $this->personModel->count_all();
	$config['per_page'] = $this->limit;
	$config['uri_segment'] = 3;
	$this->pagination->initialize($config);
	$data['pagination'] = $this->pagination->create_links();
 
	// generate table data
	$this->load->library('table');
	$this->table->set_empty(" ");
	$new_order = ($order_type == 'asc' ? 'desc' : 'asc');
	$this->table->set_heading(
		'No',
		anchor('person/index/'.$offset.'/name/'.$new_order, 'Name'),
		anchor('person/index/'.$offset.'/gender/'.$new_order, 'Gender'),
		anchor('person/index/'.$offset.'/dob/'.$new_order, 'Date of Birth (dd-mm-yyyy)'),
		'Actions'
	);
	$i = 0 + $offset;
	foreach ($persons as $person){
		$this->table->add_row(++$i, $person->name, strtoupper($person->gender)=='M'? 'Male':'Female', date('d-m-Y',strtotime($person->dob)),
			anchor('person/view/'.$person->id,'view',array('class'=>'view')).' '.
			anchor('person/update/'.$person->id,'update',array('class'=>'update')).' '.
			anchor('person/delete/'.$person->id,'delete',array('class'=>'delete','onclick'=>"return confirm('Are you sure want to delete this person?')"))
		);
	}
	$data['table'] = $this->table->generate();
 
	// load view
	$this->load->view('personList', $data);
}

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.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

4 Comments

  1. [...] This post was mentioned on Twitter by ioros, Abdullah Al Mamun. Abdullah Al Mamun said: CodeIgniter simple CRUD improved – marthijn. http://bit.ly/a4HiAK #codeigniter. [...]

  2. Stephen says:

    I assume that the ‘index‘ function is intended to be in a controller. You’re doing lots of extra work with the $this->uri->segment() calls. The arguments of the function should already be filled in by segments 3 through 5. Assuming you have your URI configured properly in your configuration file.

    See: http://codeigniter.com/user_guide/general/controllers.html#passinguri

  3. Stephen says:

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

    anchor('person/index/'.$offset.'/name/'.($order_type == 'asc' ? 'desc' : 'asc')

    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.

  4. Marthijn says:

    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