0
votes

I have a query that I use to get my data from two tables. How can I use the codeigniter active records "Join" instead of my query

public function get_categories($parent_id = 0) {       
    $query = $this->db->query("SELECT * FROM " . $this->db->dbprefix . "category c LEFT JOIN " . $this->db->dbprefix . "category_description cd ON (c.category_id = cd.category_id) WHERE c.status=1 and c.parent_id = '" . (int)$parent_id . "' order by sort_order");
    return $query->result_array();
}

Question: What would be the best to convert it to codeigniter active records "Join" function.

2

2 Answers

2
votes

Try this

$this->db->select('*');
$this->db->from($this->db->dbprefix.'category c');
$this->db->join($this->db->dbprefix.'category_description cd', 'c.category_id = cd.category_id');
$this->db->where('c.status',1);
$this->db->where('c.parent_id',$parent_id);
$this->db->order_by("sort_order", "asc");
$query = $this->db->get();
return $query->result_array();
1
votes
$this->db->select('*');
$this->db->from($this->db->dbprefix.'category c');
$this->db->join($this->db->dbprefix.'category_description cd', 'c.category_id = cd.category_id');
$this->db->where(array('c.status' => 1 ,'c.parent_id' => $parent_id));
$this->db->order_by("sort_order", "asc");
$query = $this->db->get()->result_array();
print_r($query);