0

I stumbled upon a logic problem today with Codeigniter. After 4 hours, I thought I should better ask here.

My project main page contains:

  • Top posts (Attributes: Title, Logo, Excerpt)
  • Top Commentaries for each Post (Attributes: Member-name, Avatar)

My problem is with getting the top commentaries for each post.

If I'm not using Code Igniter this is how I will do it:

<?php
foreach( $posts as $post ) {
    echo 'Post Title: '.$post['title'].'<br/>';

    $top_commenters = $this->get_top_commenters($post['id']);
    foreach( $top_commenters as $commenter ) {
        echo '<img src="'. $commenter['avatar'] .'"/><br/>';
    }
}

?>

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.

One last solution I would use is Ajax. But I hope I get some help here.

Thanks in Advance!

UPDATE: as requested, here are my Model function and Controller function:

//Model Function
function get_popular_posts() {

    $row = $this->db->query('
        SELECT * FROM posts
        INNER JOIN popular_posts ON posts.id = popular_posts.pid
        ORDER BY rank DESC LIMIT 6
    ');

    return $row->result_array();
}

//Model Function
function get_top_commenters($post_id){

    $row = $this->db->query('
        SELECT *
        FROM users
        WHERE username
        IN (
        SELECT username
        FROM comments WHERE pid = '. $post_id .')
        ORDER BY likes DESC LIMIT 6
    ');

    return $row->result_array();
}
5
  • Can you show me your controller and the model function you use to get the posts? Commented Jul 30, 2013 at 11:02
  • Please clarify why stackoverflow.com/questions/9986520/… and stackoverflow.com/questions/16366130/… and stackoverflow.com/questions/11682604/… dont answer your question. Commented Jul 30, 2013 at 11:04
  • I know why! but I need help for work around. Commented Jul 30, 2013 at 11:06
  • @Gordon I think I used the wrong title. I don't want to call a controller function from a view, I wanted help if I'm in need to do that, what's the correct way of doing it in CodeIgniter. Please read my question. Commented Jul 30, 2013 at 11:14
  • Correct way of doing what? And why does your question still say "call controller from view" And why did you add the Model code? You have to clarify more if you want me to reopen the question. Commented Jul 30, 2013 at 11:20

2 Answers 2

1

Why would you want to call controller functions from your views? By the time you generate your view file, all your data processing should be already done.

Pass the $top_commenters to your view within the controller.

Sign up to request clarification or add additional context in comments.

1 Comment

I've updated my question, please check it.
-6

you can call any controller from view: URL example :

<a href="controller_name/controller_function"> My URL </a> 

3 Comments

That's not calling a controller. That's linking to a controller.
That just links to the controller (and in turn, its view). -1
The problem here is that you need user to trigger controller function but what we need is to get result when the view is loaded i.e calling a method from another class and getting its result.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.