title: Codeigniter: Need to call a Controller function from a View
body: <p>I stumbled upon a logic problem today with Codeigniter. After 4 hours, I thought I should better ask here.</p>
  
  <p>My project main page contains:</p>
  
  <ul>
  <li>Top posts (Attributes: Title, Logo, Excerpt)   </li>
  <li>Top Commentaries for each Post (Attributes: Member-name, Avatar)</li>
  </ul>
  
  <p>My problem is with getting the top commentaries for each post.</p>
  
  <p>If I'm not using Code Igniter this is how I will do it:</p>
  
  <pre><code>&lt;?php
  foreach( $posts as $post ) {
      echo 'Post Title: '.$post['title'].'&lt;br/&gt;';
  
      $top_commenters = $this-&gt;get_top_commenters($post['id']);
      foreach( $top_commenters as $commenter ) {
          echo '&lt;img src="'. $commenter['avatar'] .'"/&gt;&lt;br/&gt;';
      }
  }
  
  ?&gt;
  </code></pre>
  
  <p>I think you noticed that I'm passing each Post's ID to a function, to get its top commenters. However, in CodeIgniter, I can't call a controller from a view. </p>
  
  <p>One last solution I would use is Ajax. But I hope I get some help here.</p>
  
  <p>Thanks in Advance! </p>
  
  <p>UPDATE: as requested, here are my Model function and Controller function:</p>
  
  <pre><code>//Model Function
  function get_popular_posts() {
  
      $row = $this-&gt;db-&gt;query('
          SELECT * FROM posts
          INNER JOIN popular_posts ON posts.id = popular_posts.pid
          ORDER BY rank DESC LIMIT 6
      ');
  
      return $row-&gt;result_array();
  }
  
  //Model Function
  function get_top_commenters($post_id){
  
      $row = $this-&gt;db-&gt;query('
          SELECT *
          FROM users
          WHERE username
          IN (
          SELECT username
          FROM comments WHERE pid = '. $post_id .')
          ORDER BY likes DESC LIMIT 6
      ');
  
      return $row-&gt;result_array();
  }
  </code></pre>