title: Codeigniter: foreach method or result array?? [Models +View]
body: <p>I am currently following tutorials on viewing data from the database using the Framework Codeigniter. There are various ways in which I've learnt. Is there is a more realiable way- either displaying as an array or using 'foreach' in the view file? Any opinions would be helpful. </p>
  
  <p>This is my code using the two methods:</p>
  
  <p>Method 1 Model:</p>
  
  <pre><code>function getArticle(){
      $this-&gt;db-&gt;select('*');
      $this-&gt;db-&gt;from('test');
      $this-&gt;db-&gt;where('author','David');
      $this-&gt;db-&gt;order_by('id', 'DESC');
      $query=$this-&gt;db-&gt;get();
  
      if($query-&gt;num_rows() &gt; 0) {
          foreach ($query-&gt;result() as $row) {
              $data[] = $row;
          }
          return $data;
      }$query-&gt;free_result();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Method 1 View file:</p>
  
  <pre><code> &lt;?php foreach($article as $row){ ?&gt;
          &lt;h3&gt;&lt;?php echo $row-&gt;title;  ?&gt;&lt;/h3&gt;
          &lt;p&gt;&lt;?php echo $row-&gt;content;  ?&gt;&lt;/p&gt;
          &lt;p&gt;&lt;?php echo $row-&gt;author;  ?&gt;&lt;/p&gt;
          &lt;p&gt;&lt;?php echo $row-&gt;date; ?&gt;&lt;/p&gt;
  &lt;?php } ?&gt;
  </code></pre>
  
  <p>Method 2 Model:
  class News_model extends CI_Model {</p>
  
  <pre><code>function getArticle(){
      $this-&gt;db-&gt;select('*');
      $this-&gt;db-&gt;from('test');
      $this-&gt;db-&gt;where('author', 'David');
      $this-&gt;db-&gt;order_by('id', 'DESC');
      $query=$this-&gt;db-&gt;get();
  
      if ($query-&gt;num_rows()&gt;0) { 
          return $query-&gt;row_array();
      }
      $query-&gt;free_result();
  }
  </code></pre>
  
  <p>Method 2 View file:</p>
  
  <pre><code>    &lt;?php echo '&lt;h3&gt;' .$article['title'].'&lt;/h3&gt;' ?&gt;
      &lt;?php echo '&lt;p&gt;' .$article['content']. '&lt;/p&gt;' ?&gt;
      &lt;?php echo '&lt;p&gt;' .$article['author']. '&lt;/p&gt;' ?&gt;
      &lt;?php echo '&lt;p&gt;'. $article['date']. '&lt;/p&gt;' ?&gt;
  </code></pre>