Earlier I’ve shown how to create a CRUD admin panel using plain php. In this article I will explain how you can do it using Codeigniter.
Steps, will be pretty much the same, even easier because I already wrote files that will automate most parts for Codeigniter.
Let’s say we need to create an admin panel for an eCommerce website, we have a products and categories db tables and we need to create a CRUD admin panel for products.

So, let’s start!

- First, download and install Codeigniter( I will be using Codeigniter 2.1.0 in this tutorial ). Make sure that you set your database connection settings in /application/config/database.php and included ‘database’ to list of libraries that will be autoloaded in /application/config/autoload.php , we will also need ‘url’ helper, so please add it to autoload as well.
- Download Data manager library, extract contents of archive in your root folder and rename extracted folder to say ‘data_manager’.
- Replace /data_manager/params.php with one from this archive. It will make Data manager retrieve database connection settings and daseurl from Codeigniter config files.
- Next step is creating Data manager profile file which will tell Data manager what exactly we need to do. For our ‘products’ db table, let’s create a /data_manager/profiles/products_admin.php profile file with following contents:

<?php
$table = 'products';

$fields = array (
	array (
		'name' => 'id'
		, 'type' => 'key'
		, 'title' => 'ID'
	)
	, array (
		'name' => 'productCode'
		, 'type' => 'input'
		, 'title' => 'Code'
	)
	, array (
		'name' => 'productName'
		, 'type' => 'input'
		, 'title' => 'Name'
		, 'is_link' => 1
		, 'link' => '/products/edit/*'
	)
	, array (
		'name' => 'category_id'
		, 'type' => 'lookup'
		, 'title' => 'Category'
		//lookup field specific parameters
		, 'lookup_table' => 'categories'
		, 'key_field' => 'id'
		, 'values_field' => 'title'
	)
	, array (
		'name' => 'productScale'
		, 'type' => 'list'
		, 'title' => 'Scale'
		, 'items' => array(
			"1:10" => "1:10( minimal )"
			, "1:12" => "1:12( tiny )"
			, "1:18" => "1:18( average )"
			, "1:24" => "1:24( big )"
			, "1:32" => "1:32( maximal )"
		)
		, 'list_mode' => 0
	)
	, array (
		'name' => 'productDescription'
		, 'type' => 'textarea'
		, 'title' => 'Description'
		, 'rows' => 5
		, 'cols' => 60
		, 'list_mode' => 0
	)
	, array (
		'name' => 'buyPrice'
		, 'type' => 'input'
		, 'title' => 'Price'
		, 'css_class' => 'product_price'
	)

	, array (
		'name' => 'is_active'
		, 'type' => 'check'
		, 'title' => 'Is active'
	)
	, array (
		'name' => 'managing'
		, 'type' => 'managing'
		, 'title' => 'Actions'
		, 'delete_link' => '/products/delete/*'
		, 'edit_link' => '/products/edit/*'
		, 'non_db' => 1
		, 'single_mode' => 0
	)
);

Variable $table is telling Data manager that we will be working with ‘products’ db table.
Variable $fields is array of fields that will be used for our admin panel. What each field does you can see from result, parameters are mostly self-explanatory, but you can also look at detailed explanation of Data manager fields parameters.

- Next step is to create controller and views. Download archive with sample controller and views and extract it to /application folder.
- Rename controller file and folder inside views folder from ‘sample_controller’ to a name of controller that you want to use. In out case let’s use controller name ‘products’.
After that open controller file ( /application/controllers/products.php ), rename controller class name to ‘Products’ and change value for class variable $dm_profile to our Data manager profile name ( ‘products_admin’ ) to make it look like this:

<?php
include_once BASEPATH.'../data_manager/DataManager.php';

class Products extends CI_Controller {
	private $dm_profile = 'products_admin';
	private $controller = '';
...

And that’s it!!!

You asking “Soooo easy?” – Yes, so easy!

So if you access http://your_domain/products you will see something like this:
admin panel built with Data manager
And if you click ‘Edit’ link in front of any item, or item title you will see this:
Item editing page built with Data manager

Here you can download complete sources of this demo project with a database.
You can change controllers and views files to make this admin panel look and work more like you want it to look, it is just a demo that shows basic usage of Data manager library.
So keep in touch with new tutorials and new versions of Data manager.

 

3 Responses to Creating CRUD admin panel in Codeigniter

  1. godhongenom says:

    how about login with data-manager password, sir? i see that the password is salted, and i dont know how to login with that password..

  2. godhongenom says:

    i see again you didnt use input password for hashing the password… could u explain this?

    • Andrey says:

      Hi! ‘password’ field in Datamanager is made so admins of the site can edit users’ passwords.
      If you want to create solted and hashed password same way Datamanager do it( for example for registration page on front end of site), you can use this code:

      $real_pass = $this->input->post('pass');
      $salt = substr(md5(time() + rand()), 0, 10);
      $hashed_pass_with_solt = md5($real_pass.$salt).':'.$salt;

      And when you check if entered password is correct, you can use this code:

      $username = $this->input->post('username');
      $pass = $this->input->post('pass');
      $this->db->from('users');
      $this->db->where('username', $username);
      $query = $this->db->get();
      $user_obj = $query->row();
      if (!$user_obj) {
      // No user with such username found
      return false;
      }
      list($hash, $salt) = explode(':', $user_obj->pass);
      if ( md5($pass.$salt) == $hash ) {
      //Username/pass combination is correct
      return true;
      }else {
      //Username/pass combination is wrong.
      return false;
      }

      Hope, this will help.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>