1

I like to convert following MySQL queries into Codeigniter Active Record Queries.

Following is MySQL:

select brand_id,name,(select count(*) from items where brand_id = b.brand_id) as itemc, (select count(*) from models where brand_id = b.brand_id) as modelc from brands as b

Codeigniter :

$this->db->limit($perpage,$page);
    $query = 'select brand_id,name,(select count(*) from items where brand_id = b.brand_id) as itemc, (select count(*) from models where brand_id = b.brand_id) as modelc from brands as b limit '.$perpage.' offset '.$page.'';
    $query = $this->db->query($query);
    $query = $query->result();
    return $query;

Kindly help me to get the above codes converted for Codeigniter Active Record.

1 Answer 1

8

here is the query in active records

 $rows =   $this->db->select('b.brand_id,b.name')
    ->select('(select count(*) from items where brand_id = b.brand_id) as itemc',FALSE)
    ->select('(select count(*) from models where brand_id = b.brand_id) as modelc',FALSE)
    ->from('brands as b')
    ->limit($perpage,$page)->get()->result();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.