codeignitergrocery-crud

GroceryCrud + CodeIgniter change value inside field


I'm using GroceryCrud for save data. User register is from website itself. When select their sex, i'm saving 1 for male, 2 for female. Database field is tinyint. So problem is, when admin view their data from backend, it's obvious 1 or 2 will appear on Sex field. How to change it into male, female depending on value?

enter image description here


Solution

  • You can use the callback_column for this.

    In your case you can do:

    public function webpages()
    {
      $c = new grocery_CRUD();
    
      $c->set_table('users');
      $c->columns('firstname','lastname','sex','age');
    
      $c->callback_column('sex',array($this,'_callback_sex'));
    
      $output = $c->render();
      $this->_view_output($output);
    }
    
    public function _callback_sex($value, $row)
    {
        if ($value == '2') {
            return 'male'
        } elseif ($value == '1') {
            return 'female';
        } else {
            return "-";
        }
    }