Recently I was trying to read the default values I had set on the fields of a table in my database.
Since it was a succesful way of doing it I thought to share it with you.
It invloves:
1. creating a model that extends the CI_Model with the following code
/* make a file under application/models/ called db_model.php */ <?php class db_model extends CI_Model { public function get_default($table, $field) { $query = "SHOW COLUMNS FROM `" . $table . "` WHERE field = '" . $field . "'"; $result = $this->db->query($query); return $result; } }
2. within your controller's function declaring a callback_field
public function my_controller() { ... $crud = new Grocery_CRUD(); $crud->set_table('my_table'); $crud->callback_field('a_field',array($this,'cb_a_field')); ... }
3. coding the actual callback for each field
function cb_a_field($v, $pk = null) { $this->load->model('db_model'); $f = $this->db_model->get_default('my_table', 'a_field')->field_data(); foreach($f as $field) { $v = $field->default; } $return_value = '<div id="field-a_field" class="xxx">' . $v .'</div>'; return $return_value; }
The $return_value can be any valid html component. Check here the ones that Grocery CRUD supports out of the box.