CodeIgniter Forums
complex query - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: complex query (/showthread.php?tid=18303)



complex query - El Forum - 05-01-2009

[eluser]cristian_c[/eluser]
Is there a better way to do this. This is working ok, but I'm thinking there has to be a better way.
Basically I need to add the comments to the result of the blogs query.
Am I breaking any standards or rules by having a $this->getComments($post['id']); inside a foreach?


Code:
function getBlogs()
    {
        $query = $this->db->get('blogs');
        $result = $query->result_array();
            foreach($result as $i => $blog){
                $result[$i]['comments'] = $this->getComments($post['id']);
            }
            return $result;
    }
    
    
    function getComments($church_id)
    {
        $this->db->where('post_id',$post_id);
        $query = $this->db->get('comments');
        return $query->result_array();
        
    }



complex query - El Forum - 05-01-2009

[eluser]Matthieu Fauveau[/eluser]
Code:
$this->db->select('b.item1, b.item2, c.item1, c.item2, etc...')->from('blogs b')->join('comments c', 'c.post_id = b.post_id')->get();

Something like that Wink