title: How To Make Codeigniter Query Result Into a Table
body: <p>I would like to create HTML table with my query results. The HTML table should look like this:</p>
  
  <p><a href="https://i.stack.imgur.com/Vs4f4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Vs4f4.png" alt="enter image description here"></a></p>
  
  <p>I don't really understand about Codeigniter's HTML Table Class, so I just create a loop for each column of the table on my view like this:</p>
  
  <pre><code>&lt;tr&gt;
  &lt;?php foreach ($nameloop as $row) { ?&gt; &lt;td&gt;&lt;?php echo $row-&gt;username; ?&gt; &lt;/td&gt;&lt;?php } ?&gt;
  &lt;?php foreach ($addressloop as $row) { ?&gt; &lt;td&gt;&lt;?php echo $row-&gt;useraddress; ?&gt; &lt;/td&gt;&lt;?php } ?&gt;
  &lt;?php foreach ($ageloop as $row) { ?&gt; &lt;td&gt;&lt;?php echo $row-&gt;userage; ?&gt;&lt;/td&gt; &lt;?php } ?&gt;
  &lt;?php foreach ($sexloop as $row) { ?&gt; &lt;td&gt;&lt;?php echo $row-&gt;usersex; ?&gt;&lt;/td&gt; &lt;?php } ?&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>Turns out it was a very bad decision. It makes my table look horizontally instead vertically like it used to.</p>
  
  <p>Here's my model:</p>
  
  <pre><code>&lt;?php class Front_model extends CI_Model {
  
          public function __construct()
          {
                  parent::__construct();
                  $CI = &amp;get_instance();
                  $this-&gt;db2 = $CI-&gt;load-&gt;database('db2', TRUE);
          }
  
  
          function namelist()
          {
              $query = $this-&gt;db-&gt;query("SELECT name as uername FROM table1 ORDER BY name");
              return $query-&gt;result();
          }
  
          function addresslist()
          {
              $query = $this-&gt;db-&gt;query("SELECT address as useraddress FROM table1 INNER JOIN name
                                         ON address.id = name.id GROUP BY id ORDER BY name");
              return $query-&gt;result();
          }
  
          function agelist()
          {
              $query = $this-&gt;db-&gt;query("SELECT age as userage FROM table1 INNER JOIN name
                                         ON age.id = name.id GROUP BY id ORDER BY name");
              return $query-&gt;result();
          }
  
          function sexlist()
          {
              $query = $this-&gt;db-&gt;query("SELECT sex as usersex FROM table1 INNER JOIN name
                                         ON sex.id = name.id GROUP BY id ORDER BY name");
              return $query-&gt;result();
          }
  
  }
  </code></pre>
  
  <p>And then here's my function on my CONTROLLER:</p>
  
  <pre><code>public function index()
      {
  
          $data['nameloop'] = $this-&gt;front_model-&gt;namelist();
          $data['addressloop'] = $this-&gt;front_model-&gt;addresslist();
          $data['ageloop'] = $this-&gt;front_model-&gt;agelist();
          $data['sexloop'] = $this-&gt;front_model-&gt;sexlist();
  
  
          $this-&gt;load-&gt;view('home',$data);
      }
  </code></pre>
  
  <p>This make me quite confused. Whether it's the HTML tag or I code it wrong.. I don't know...
  Thank you for your help, guys...</p>