⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

relation 1-n - Concatenate related rows in column



MiamMiam
  • profile picture
  • Member

Posted 01 January 2014 - 10:46 AM

Hi,

 

My question is based on the "examples" database delivered with GC.

Let's say I would like to concatenate the names of the employees located in the same city in the Offices list.

 

For example, for Boston, there would be a new column containing "Firrelli Julie, Patterson Steve".

 

How to manage this ? With the "column" function? with a callback ? Thanks

 

Happy new year !

 


MiamMiam
  • profile picture
  • Member

Posted 15 January 2014 - 16:20 PM

I've found the solution ! (might seem easy but it's been a while since I've programmed in Php and I'm new to CI and OOP).

 

In the "offices" function :

        $crud->callback_column('employees',array($this,'_column_employees'));

Somewhere else in the controller :

   function _column_employees($primary_key , $row)
    {
        $select = "SELECT lastName, firstName FROM `employees`  WHERE officeCode='" . $row->officeCode . "'";
        $query = $this->db->query($select);
        $employees = '';
        foreach ($query->result() as $name)
        {
           $employees .= $name->lastName . ' ' . $name->firstName . ', ';
        }
        return $employees;
  }

It works. Maybe it could be optimized. Any idea ?