title: Codeigniter - Query inside foreach
body: <p>So i'm using latest Codeigniter and my problem is that i have this situation where i should use query inside foreach in controller. I have view that shows all production orders in one list, and i want all user made comments and notes underneath them from other table. Quite general situation in coding, but how i should do this with Codeigniter, cause you should keep your SQL clauses in Models. </p>
  
  <p>Before i just fork raw SQL inside controller, i wanted to ask what is more gentle and proper way to achieve this. Thanks!</p>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>&lt;?php
  public function show()
  {    
    $this-&gt;load-&gt;database();
  
    $this-&gt;load-&gt;model('report_model');
    $this-&gt;load-&gt;helper('url');
  
    $data['productionOrders'] = $this-&gt;report_model-&gt;getAllProductionOrders();
    $this-&gt;load-&gt;view('all_reports', $data);
  }
  ?&gt;
  </code></pre>
  
  <p><strong>Model</strong></p>
  
  <pre><code>&lt;?php
  function getAllProductionOrders()
  {
    $this-&gt;db-&gt;select(*);
    $this-&gt;db-&gt;from('dbo.QualityControl_ProductionOrders');
  
    $query = $this-&gt;db-&gt;get();
  
    return $query-&gt;result();
  }
  ?&gt;
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  
   &lt;head&gt;
     &lt;meta charset="utf-8"&gt;
   &lt;/head&gt;
  
   &lt;body&gt;
  
    &lt;?php
      foreach ($productionOrdersas $row)
      {
    ?&gt;
  
    &lt;div&gt; 
      ProdNo: &lt;?php echo $row-&gt;Prodno; ?&gt;
      Descriptions: 
      Hours:
      etc etc etc etc
    &lt;/div&gt;
  
    [I want here comments and all user made notes]
  
    &lt;?php
      }
    ?&gt;
   &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>