join in codeigniter code example

Example 1: how to use join query in codeigniter

public function getdata(){	

	$this->db->select('*');
	$this->db->from('table1'); // this is first table name
	$this->db->join('table2', 'table2.id = table1.id'); // this is second table name with both table ids
	$query = $this->db->get();
	return $query->result();

	}

Example 2: codeigniter where order by

$this->db->select('ae_users.employee_id, ae_users.emp_name, ae_users.emp_name2, ae_users.emp_name3');
		$this->db->from('ae_users');                
		$this->db->where_in('ae_users.employee_id',$employee_ids);
                $this->db->_protect_identifiers = FALSE; // stop CI adding backticks
                $order = sprintf('FIELD(ae_users.employee_id, %s)', implode(', ', $employee_ids));
                $this->db->order_by($order);
                $this->db->_protect_identifiers = TRUE; 
		$query =$this->db->get();
               // echo '<pre>'; print_r($this->db->last_query());exit;
		if ($query->num_rows()) {
			return $query->result_array();
		} else {
			return 0;
		}

Tags:

Php Example