0

I have my function to get a list of departments where i do a left join from 2 tables, i want to know how can i make the equivalent on codeigniter way? my code is this:

 function get_department_list()
 {
      $sql = '
      SELECT
      a.employee_name,
      b.department_name
      FROM
      tbl_employee a 
      LEFT JOIN tbl_department b ON b.department_id = a.department_id';

      $query = $this->db->query($sql);
      $result = $query->result();
      return $result;
 }

I was reading the codeigniter documentation and tried to do it the CI way but i only get errors, this is how i tried to do it:

 function get_department_list()
 {
      $sql = '
      $this->db->select('a.employee_name','b.department_name');
      $this->db->from('tbl_employee a');
      $this->db->join('tbl_employee b', 'b.department_id = a.department_id', 'left');
      $query = $this->db->get();

      $query = $this->db->query($sql);
      $result = $query->result();
      return $result;
 }

This is the error:

Parse error: syntax error, unexpected 'a' (T_STRING) in /Applications/MAMP/htdocs/CI-Employee/application/models/Department_model.php on line 13 A PHP Error was encountered

Severity: Parsing Error

Message: syntax error, unexpected 'a' (T_STRING)

Filename: models/Department_model.php

Line Number: 13

Backtrace:

Any help will be really appreciated thanks guys!

EDIT:

This was my solution and thanks guys for your time!

 function get_department_list()
 {
      $this->db->select('a.employee_name,b.department_name');
      $this->db->from('tbl_employee a');
      $this->db->join('tbl_department b', 'b.department_id = a.department_id', 'left'); 
      $query = $this->db->get();
      return $query->result();
 }
Carlos Sanchez
  • 35
  • 1
  • 2
  • 12
  • A PHP Error was encountered Severity: Notice Message: Undefined property: stdClass::$department_name Filename: views/department_view.php Line Number: 24 Backtrace: File: /Applications/MAMP/htdocs/CI-Employee/application/views/department_view.php Line: 24 Function: _error_handler File: /Applications/MAMP/htdocs/CI-Employee/application/controllers/Department.php Line: 19 Function: view – Carlos Sanchez Nov 16 '15 at 04:16
  • line 19 is this one: department_name; ?> – Carlos Sanchez Nov 16 '15 at 04:16
  • and this is not a duplicate its not near similar of what i am asking for help – Carlos Sanchez Nov 16 '15 at 04:18

0 Answers0