In case you've missed it, you are looking at an older version of the website. Checkout our latest version, we promise you will love it 😍

Tutorial - Basic methods

Many users understand the simplicity of using grocery CRUD but after installing it they don't actually know where to start. In this tutorial we will try to cover the basic functionality of grocery CRUD and a step by step tutorial of how using it.

First of all make sure that you have already install grocery CRUD in your project by simply following the steps at grocery CRUD installation or the newbies grocery CRUD installation guide

Let's start with an example table to know exactly what we are expecting to have as a result. Let's say we have the below table:

 
CREATE TABLE IF NOT EXISTS `employees` (
  `employeeNumber` int(11) NOT NULL AUTO_INCREMENT,
  `lastName` varchar(50) NOT NULL,
  `firstName` varchar(50) NOT NULL,
  `extension` varchar(10) NOT NULL,
  `email` varchar(100) NOT NULL,
  `officeCode` varchar(10) NOT NULL,
  `file_url` varchar(250) CHARACTER SET utf8 NOT NULL,
  `jobTitle` varchar(50) NOT NULL,
  PRIMARY KEY (`employeeNumber`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1703 ;
 

First of all we create our function at the controller. Let's say we have the controller Demo and we want to create our function employees_example so we will have something like this:

 
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Demo extends CI_Controller {
 
    function __construct()
    {
        parent::__construct();
 
        $this->load->database();
 
    }
 
    public function index()
    {
 
    }
 
    public function employees_example()
    {
        $crud = new grocery_CRUD();
 
        $crud->set_table('employees');
        $output = $crud->render();
 
        $this->_example_output($output);                
    }
 
    function _example_output($output = null)
 
    {
        $this->load->view('our_template.php',$output);    
    }    
}
 
/* End of file main.php */
/* Location: ./application/controllers/main.php */?>
 
Did you know that you can pass your own custom variable to codeigniter $this->load->view combined with grocery CRUD? If you are interested of how to do that, you can read the forums post for 3 different options of how to do that.

set_table

A good starting point is to have always a method like this:

 
    public function my_function_name()
    {
        $crud = new grocery_CRUD();
 
        $crud->set_table('your_table_name');
        $output = $crud->render();
 
        $this->_example_output($output);                
    }
 
As we can see at all our examples we use the $crud->function_name . We can also use grocery CRUD with the default library functionality of Codeigniter like this: $this->grocery_crud->function_name . Though it is not suggested as with $this->grocery_crud-> we can not have the autocomplete from our editor. So it is suggested to use our methods with the way that the examples are.

So we can copy it easily and have a starting point to use grocery CRUD. In our example we first have to see the CRUD is rendered without any problem with the default functionality. So when we will add those 4 simple lines of code at our project we will have a result that will look similar to this:

example-1

Now let's add our first record just to have at least one item to work with.

Always make sure that we have at least one primary key at our table. It is suggested to have your primary key as an auto increment. If you though don't have an auto-increment primary key at your table grocery CRUD will work as expected but you will not have some automaziations that grocery CRUD offers.

set_subject

Now that we added our first record and we are ready to continue. Let's first start with the most common method

$crud->set_subject('your_subject_name');

So in our case we will have:

 
    public function employees_example()
    {
        $crud = new grocery_CRUD();
 
        $crud->set_subject('Employee');
        $crud->set_table('employees');
        $output = $crud->render();
 
        $this->_example_output($output);                
    }
 

As we can easily see now ALL the "Record" strings are transformed to "Employees". So now for example we have: "Add Employee", "Edit Employee", "Delete Employee" , "Are you sure that you want to delete this Employee" and so on...

columns

The second most common method is the:

$crud->columns('field_name1','field_name2','field_name3','field_name4');

In our example we will use the:

$crud->columns('lastName','firstName','email','jobTitle');

So with this line of code we will have only 4 columns at our list page and not all the columns of the table. The add and edit form they will still have all the fields of the table without any problem.

fields

However if we still want to have less fields than the table we can simply use:

$crud->fields('field_name1','field_name2','field_name3','field_name4');

So in our case we will have:

 
    public function employees_example()
    {
        $crud = new grocery_CRUD();
 
        $crud->set_table('employees');
        $crud->columns('lastName','firstName','email','jobTitle');
        $crud->fields('lastName','firstName','extension','email','jobTitle');
 
        $output = $crud->render();
 
        $this->_example_output($output);                
    }
 
The only basic difference between fields and columns is that the columns method is used only for the list page (datagrid) and the fields method is only used for CRUD forms.

display_as

Till now every field has a default label of the database field. If you though want to change that you can simply use:

$crud->display_as('field_name','field_label');

In our case we will have:

 
        $crud->display_as('lastName','Last Name');
        $crud->display_as('firstName','First Name');
        $crud->display_as('jobTitle','Job Title');
 

So a summary of our first CRUD with the most common methods is done with the below code:

 
    public function employees_example()
    {
        $crud = new grocery_CRUD();
 
        $crud->set_table('employees');
        $crud->columns('lastName','firstName','email','jobTitle');
        $crud->fields('lastName','firstName','extension','email','jobTitle');
 
        $crud->display_as('lastName','Last Name');
        $crud->display_as('firstName','First Name');
        $crud->display_as('jobTitle','Job Title');
 
        $output = $crud->render();
 
        $this->_example_output($output);                
    }

You can also check a live example below of what those lines do.

For more examples you can also check the "Simple Examples" at grocery CRUD examples

Note: The below example is an iframe so it might appeared with a scroll bar. If you like you can view the example on a new tab