[eluser]JasonS[/eluser]
Hi, I have a function and I was wondering if someone could help me improve it. The problem is that it uses 1 query to get the company names, then an additional query for each company to count the number of coupons related to said company.
Is there a way I can combine this into one query or perhaps structure it better? I am a bit concerned about what will happen if my client creates 50 or a 100 featured companies.
Code:
// Get Featured Companies
public function get_featured_companies()
{
// Define Container
$data = array();
// Grab featured companies
$companies = $this->db->where('featured', 1)->get('companies');
if ($companies->num_rows() > 0)
{
// Loop through SQL results
foreach ($companies->result() as $company)
{
$coupons = $this->db->select('id')->where('company', $company->id)->get('coupons');
$data[] = array (
'id' => $company->id,
'name' => $company->name,
'coupons' => $coupons->num_rows()
);
}
}
return $data;
}