Concatenate two or more fields into one field?
- Single Page
Posted 21 May 2012 - 12:40 PM
I have tried following but it is not works:
$crud->columns('id', 'CONCAT(\'First_name\', \' \', \'Last_name\') as Full_name','DOB','City', 'Status');
How can I used CONCAT operation in grocery crud?
Posted 21 May 2012 - 14:07 PM
Then do something like this in the model:
[left]
$this->db->select("CONCAT(firstname, '.', lastname) AS full_name", FALSE);[/left]
That should display it, but I don't know if it'll allow you to edit it with that alone. I think you have to use callbacks afterwards.
Posted 05 September 2012 - 07:30 AM
[color=#ff0000]$crud[/color]->set_relation('[color=#008000]id[/color]','[color=#008000]students[/color]','{[color=#008000]first_name[/color]} {[color=#008000]last_name[/color]}');
[color=#ff0000]$crud[/color]->display_as('[color=#008000]student_id[/color]','[color=#008000]Students Name[/color]');
just like that...hope this will help....
Posted 05 September 2012 - 14:14 PM
If they aren't, one option is to create an SQL View:
CREATE VIEW User_view AS
SELECT id, CONCAT(First_name, ' ', Last_name), 'DOB', 'City', 'Status' FROM User;
Then you need to specify to GroceryCrud what the primary key of your view is before using it:
$crud->set_primary_key('User_view', 'id');
Another method is using a callback:
// Added a new column called 'fullname'
// First_name and Last_name need to be included to be able to reference them in the callback
$crud->columns('id', 'Full_name', 'First_name', 'Last_name', 'DOB', 'City', 'Status');
// unset the other name columns from view
$crud->unset_columns('First_name', 'Last_name');
$crud->callback_column('Full_name',array($this,'_cb_col_full_name'));
public function _cb_col_full_name($value, $row)
{
$str = $row->First_name.' '.$row->Last_name;
return $str;
}
Hope all the code works, just typed without checking it...
I don't have access to my dev environment right now...
Posted 06 June 2014 - 10:31 AM
@saulimus
The callback code didn't work for me, nothing was displayed because the "Full_name" column doesn't exist. However, you can easily just put the data in an existing field and rename that.
My code (the relevant parts only):
//Table setup $crud->columns('first_name','last_name');
//Callback function public function _cb_col_last_name($value, $row) { return htmlentities($row->first_name.' '.$value,ENT_QUOTES,'utf-8'); }
The drawback of the callback approach is that only the last name is searchable. However, merging fields in a view is often not an option because it only works if you don't need to edit your data.