Welcome Guest, Not a member yet? Register   Sign In
complex query
#1

[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();
        
    }
#2

[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




Theme © iAndrew 2016 - Forum software by © MyBB