Just dumb man try talk about code.

Friday, July 8, 2016

CodeIgniter is one of popular PHP framework that implemented MVC software architectural pattern. Its means required three steps to create one page; first, you need to write a model, then a function in the controller, and finally a view. The conventional way to create a CRUD operation in CodeIgniter is create four function in controller and four view file. You can do that faster with the help of GroceryCrud library. In this tutorial, we will create CRUD operation using GroceryCrud with only one function in controller and a layout view.

To use GroceryCrud properly, just follow the instructions below:

STEP 1     Download GroceryCrud
Go to the GroceryCrud Download Page to download compressed file.

STEP 2     Extract GroceryCrud
After the file success downloaded, extract the content to the CodeIgniter project's folder. In this example, i have a CodeIgniter project named as myapp. If you does not know how to build CodeIgniter project, please read Build Simple CRUD Application Using CodeIgniter and the go back to this step.



STEP 3     Create The Controller
This controller will handle CRUD operation on Employee table:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Employee extends CI_Controller {

 public function __construct()
 {
  parent::__construct();

  $this->load->database();
  $this->load->helper('url');

  $this->load->library('grocery_CRUD');
 }

 public function index()
 {
  $crud = new grocery_CRUD();

  $crud->set_theme('datatables');
  $crud->set_table('employees');
  $crud->set_relation('officeCode', 'offices', 'city');
  $crud->display_as('officeCode', 'Office City');
  $crud->set_subject('Employee');

  $crud->required_fields('lastName');

  $crud->set_field_upload('file_url', 'assets/uploads/files');

  $output = $crud->render();

  $this->load->view('grocery_layout.php', $output);
 }
}

STEP 4     Create The View
In this tutorial, we only need layout view named as grocery_layout.php and give the datatable theme:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
<?php 
foreach($css_files as $file): ?>
 <link type="text/css" rel="stylesheet" href="<?php echo $file; ?>" />
<?php endforeach; ?>
<?php foreach($js_files as $file): ?>
 <script src="<?php echo $file; ?>"></script>
<?php endforeach; ?>
<style type='text/css'>
body
{
 font-family: Arial;
 font-size: 14px;
}
a {
    color: blue;
    text-decoration: none;
    font-size: 14px;
}
a:hover
{
 text-decoration: underline;
}
</style>
</head>
<body>
 <div style='height:20px;'></div>  
    <div>
  <?php echo $output; ?>
    </div>
</body>
</html>

STEP 5     Run It !
If you just developing on local server, just go to localhost/myapp/index.php/employee


, ,

0 comments :

Post a Comment

Powered by Blogger.

Followers