title: CodeIgniter Performance load view multiple times vs loop in view
body: <p>I'm trying to solve possible scaling performance issue. Is there a more clear performance benefit to these two scenarios:</p>
  
  <h2>Scenario 1:</h2>
  
  <p>Make several calls to load a view and pass a small amount of data each time</p>
  
  <p>Controller</p>
  
  <pre><code>public function index()
  {
      $this-&gt;load-&gt;view('header');
      $this-&gt;load-&gt;view('table_head');
      $results = $this-&gt;db-&gt;select('*')-&gt;from('table')-&gt;get()-&gt;result_array();
      foreach ($results as $key)
      {
          $this-&gt;load-&gt;view('table_row', $key);
      }
      $this-&gt;load-&gt;view('table_foot');
      $this-&gt;load-&gt;view('footer');
  }
  </code></pre>
  
  <p>View table_head</p>
  
  <pre><code>&lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;...&lt;/th&gt;
              &lt;th&gt;...&lt;/th&gt;
              ...
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
  </code></pre>
  
  <p>View table_row</p>
  
  <pre><code>        &lt;tr&gt;
              &lt;td&gt;&lt;?php echo $var_a; ?&gt;&lt;/td&gt;
              &lt;td&gt;&lt;?php echo $var_b; ?&gt;&lt;/td&gt;
              ...
          &lt;/tr&gt;
  </code></pre>
  
  <p>View Table_foot</p>
  
  <pre><code>    &lt;/tbody&gt;
      &lt;tfoot&gt;
          &lt;tr&gt;
              &lt;td&gt;...&lt;/td&gt;
              &lt;td&gt;...&lt;/td&gt;
              ...
          &lt;/tr&gt;
      &lt;/tfoot&gt;
  &lt;/table&gt;
  </code></pre>
  
  <h2>Scenario 2:</h2>
  
  <p>Make 1 call to load a view and pass a large amount of data</p>
  
  <p>Controller</p>
  
  <pre><code>public function index()
  {
      $this-&gt;load-&gt;view('header');
      $this-&gt;data['results'] = $this-&gt;db-&gt;select('*')-&gt;from('table')-&gt;get()-&gt;result_array();
      $this-&gt;load-&gt;view('table', $this-&gt;data);
      $this-&gt;load-&gt;view('footer');
  }
  </code></pre>
  
  <p>View table</p>
  
  <pre><code>...
  &lt;table&gt;
      &lt;thead&gt;Header Info...&lt;/thead&gt;
      &lt;tbody&gt;
          &lt;?php foreach ($results as $key): ?&gt;
              &lt;tr&gt;echo row information as needed&lt;/tr&gt;
          &lt;?php endforeach; ?&gt;
      &lt;/tbody&gt;
  &lt;table&gt;
  ...
  </code></pre>
  
  <p>I've been using scenario 1 because it is helping to keep my application modular and keep the MVC format by keeping logic loops in the controller. But say the result from the SQL call is 1000 entries, or even more, is there a clear performance difference between the two scenarios? Am I attempting to make my application too modular? I do try to recycle as much of my code as I can, and as a result more of my methods will load at minimum 8 views.</p>