Getting started with Codeigniter - Creating a CRUD Application

2:19:00 PM


You can find the Source code of this article here.


Howdy Guys?This time I'll bring you another awesome PHP framework.CodeIgniter you heard it don't you? Well in this tutorial I  am going to discuss basic configuration we need to have in CodeIgniter and then we move on to create a simple CRUD application.
It is a cool little framework. It is so popular among the developers because of it's simple and easy structure which provide a way to build basic operations fast and with less coding.Also it's a good framework for developers who familiar with PHP but getting start on a PHP framework for the first time.
Install the CodeIgniter if you don't have done it already.It's a really simple process and follow this if you need any help.
Now we'll look at file structure inside CodeIgniter. There is 3 main folders and index.php file.
  • application - This is where we create and update all the application specific changes.
  • system - This contains the core of the Framework.
  • user_guid - This contains the documentation of the CodeIgniter. 
  • index.php - serves as the front controller, initializing the base resources needed to run CodeIgniter. Also it allows you to change the name and path  of the application and sustem folders.
Ok folks let's get stat with configuring the database.Open Application/config/database.php file and change below values according to your database configuration.


Well not so much crux there ay? Just a simple configuration setting that any developer can understand.
It's time to create our first Controller. Go to application/controller folder and create Customer.php file.


What it does is create a new model by extending CI_Model and load the database library.So that you have the database class available via $this->$db object.
Then we'll create a method to get list of all the customer detail or one specific customer detail.


Here we use CodeIgniter's  Query Builder.It make it possible to write your query once and make it use on all the available databases.To get good idea about Query Builder I recommend you to read this article.
$query = $this->db->get('customer'); line is same as select * from customer
and this
$query = $this->db->get_where('customer', array('username' => $username)); is same as
select  * from customer where username='$username';
Pretty simple, Isn't it. Ok Now we are going to create the controller.Go to application/controller and create Customer.php file.


Here we create a Controller call 'Customer' and extend CI_Controller class.This controller is what become the center of your every request to your app.
Then the  __construct() method: It calls the constructor of its parent class (CI_Controller) and loads the model, so it can be used in all other methods in this controller.
index method, First it invoke the get_customer method then assign a title to the page and finally load the view.
The view method,Unlike index method it pass $username as a argument and same as index method it calls the get_customer method,assign a value to the $title and load the view page.

Now lets create two view pages for these two  method.Go to application/view folder and create a folder name customer inside that create a index.php file for index method.


Then create a view.php file for view method.


Since we are in view section we'll create another two pages for create and edit purpose.create a create.php file.


Then create edit.php file.


form_open function is use to render the form element and it's provide by form helper.form_validation is use to report errors related to form validation.
Go back to Customer.php and create a new method  name create and add below code.


First we load the form helper which is use by form_open and then we load the form validation library.After loading those two we use set_rules method to add form validation rules.
 $this->form_validation->set_rules('username', 'Username', 'required');
First argument is for name of the input field, second one is for the name that should use in error message and last  one is the rule.
after form validation and if it's according to the rules we set method will invoke the set_customer method.
Now lets create edit method.


It's almost same as create method.only difference is,It take $username as argument and call get_customer method to show the initial values of the customer that you select and it invoke the update_customer method.
Since we are done with model methods we'll create set_customer method and update_customer methods.Open the Customer_model.php and create set_customer method.


Here we first take POST values and store it in $data  array and we insert it to the customer table.
Finally we'll add the delete functionality to our app.In Customer_model.php create a method name delete_customer and add below code.
Inside Cutomer.php file create method call delete and put below code segment.
Guys we are almost over only thing left if create proper routing configuration.If you  look at .htascces file inside application folder.It deny all the....
So we'll create a  new .htaccess file in our root folder.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Then go to application/config and open config.php file.change
$config['index_page'] = 'index.php';
to
$config['index_page'] = "";
Ok.Now we set the rules for routes.
open routes.php file inside application/config folder change the $route arrays values as below
$route['customer/index'] = 'customer/index';
$route['customer/create'] = 'customer/create';
$route['customer/view'] = 'customer/view';
$route['customer'] = 'customer';
$route['(:any)'] = 'customer/index';
$route['default_controller'] = 'customer/index';
I guess it's simple to understand and if you want to know more about CodeIngiter routing reffer this article.
So if you open your browser and go to 
'Your_server'/root/customer or 'Your_server'/root/customer/index
 will navigate you to the customer index(list) page and if you click any (View/Edit/Delete) link on Customer List it will bring you to the that particular customer operations respectively.

Note 
  1. I have add two more view files in source code project. header.php and footer.php. As name suggest those two will serve as header and footer for the each page. 
  2. I created another view file success.php to show a success message when ever operation is finished without errors.
  3. I used bootstrap CSS and update font end html tags accordingly, to have a finished product.  




You Might Also Like

9 comments

  1. SQL query for creating tables (so that I can fill database properly for this app) please! Awesome article.

    ReplyDelete
  2. We can automatically create dynamic crud in codeigniter and other frameworks using http://crudgenerator.in

    ReplyDelete
  3. Thanks for sharing it !!!
    Really awesome blog. Keep blogging and updating.
    Software company in India

    ReplyDelete
  4. This is a clear and step by step documentation.we can generate codeigniter crud with our table name and table fields http://phpcodebuilder.com/crud-generator

    ReplyDelete
  5. this post demonstrates clearly about crud operation in codeigniter.

    If you need to remove /index.php/ you may follow this steps.

    Remove index.php from codeigniter url

    ReplyDelete
  6. Hi there,

    Check out this blog, full angularjs CRUD with pagination sorting and filter using codeigniter as backend framework

    You can even download and run on your local system. here is the link

    https://angularjscrudpagination.blogspot.com/

    ReplyDelete
  7. This is a great blog.
    Being a PHP developer I prefer to use Codeigniter framework for webdevelopment. But still if anyone of you facing any problem then contact darkbears and hire codeigniter developer.

    ReplyDelete

Popular Posts