As you can see, using Codeigniter Active Records is very simple. The important fact to remember is that active records belong to the model and not to the controller or view.


Here is a sample PHP code snippet that gets the results:


application/models/CustomerModel.php

<?php

class CustomerModel extends CI_Model {

    function get_result($id) {

        //elect one or more columns from the table
        $this->db->select('name, email, contact');

        //select one or more tables from the database.
        $this->db->from('customer');

        //joins for tables.
        $this->db->join('departments', 'customer.id = operator.id');
        $this->db->where('id', $id);
        $this->db->get();
        return $query->result();
    }

}

$this->db->get()

// Produces: SELECT * FROM mytable
$query = $this->db->get('mytable');  

set a limit and offset clause:

// Executes: SELECT * FROM mytable LIMIT 20, 10
$query = $this->db->get('mytable', 10, 20); 

$this->db->select()

// Executes: SELECT title, content, date FROM mytable
$this->db->select('title, content, date');
$query = $this->db->get('mytable');


$this->db->from()

 // Produces: SELECT title, content, date FROM mytable
$this->db->select('title, content, date');
$this->db->from('mytable');
$query = $this->db->get();

There is lots of active queries we will discus more in later but now you have understood active Query Builder