0
votes

Okay so, I'm working with CodeIgniter. posts.php is my view that displays all the posts, each post must display its corresponding comments, which is what I'm trying to achieve.

I have a method in my model that takes the postid($postid) and return its corresponding comment($comment), unless I call the model method via a controller method,how do I accompolish this?

This is my view :

<body>
    <?php foreach ($post as $key):?>
    <div class="container">
        <div class="span10">

        <div id="box" class="alert-message block-message info">
            <div id="post" class="post">
                <?php echo $key->content;?><br />
            </div>
            <div>
            <p><?php //echo $comment;?></p> <!--HERE THE COMMENTS OF THE CORRESPONDNING POST MUST BE ECHOED-->
            </div>
            <div>
            <a href="#" id="commentnow<?php echo $key->postid;?>"><p><em>Comment</em></p></a>
            </div>
            <div id="commentarea<?php echo $key->postid;?>">
            <?php $name=array('name'=>"form$key->postid");
                  echo form_open("/welcome/comments/$key->postid",$name);
                  $data=array(
                                'id' => 'input',
                                'name'=> 'content',
                                'rows' => '2',
                                'placeholder' => "Write a comment...",
                                'autofocus' => 'TRUE'             
                      );
                  echo form_textarea($data);    

            ?>
            <a href="JAVASCRIPT:form<?=$key->postid;?>.submit()" id="cbtn" class="btn primary small">Comment</a>
            <?=form_close();?>
            </div>
        </div>
    </div>

    </div>
    </div>
    <script type="text/javascript">
    $(function(){
    $("div#commentarea<?=$key->postid;?>").hide(); 
    $('a#commentnow<?=$key->postid;?>').click(function(){

        $("div#commentarea<?=$key->postid;?>").slideToggle(250);
        }); 
    });

    </script>
    <?php endforeach;?>
    </body>

This is my controller method that returns the comments that corresponds to the postid:

public function comments($postid)
{

    //Post Comment
    $comment=$this->input->post('content');
    $data=array('content'=>$comment,'comment_postid'=>$postid);
    $this->comments->postcomment($data);

    //Retrieve
    $comments['comment']=$this->comments->retrieve($postid);
    $this->load->view('posts',$comments);

}

I'm a newbie,pardon me if my code is bad. I'm always looking forward to improving my code> Thanks for being patient. :)

1
? you pass them to the view as you pass the posts, don't see the problem. Please be clearer and specify exactly what's the issue - Damien Pirsy
I'm sorry. I've edited it to make it clearer. - Arvind
To answer your headline question. It's impossible to call a controller from a view in CI. It's just not built to work that way. Like @DamienPirsy said, load the comments in your controller and pass them to the view. - danneth

1 Answers

0
votes

Looking at your code it seems to me that you haven't fully understood how to use the MVC.

Firstly, your controller method comments contains the extraction of comments AND adding comments to a post. This doesn't seem logical when taking a look at the view file.

Instead you should seperate those two.

In your controller, add another metod called *post_comment* and move the adding comment functionality to that method and add a redirection afterwards:

public function post_comment($postid)
{

    //Post Comment
    $comment=$this->input->post('content');
    $data=array('content'=>$comment,'comment_postid'=>$postid);
    $this->comments->postcomment($data);

    redirect('welcome/comments'); //redirect back to comments
}

Now, remove the adding of a comment from your comment method in the controller, so that you only retrieve the comments:

public function comments($postid)
{

    //Retrieve
    $comments['comment']=$this->comments->retrieve($postid);
    $this->load->view('posts',$comments);
}

And finally change your view file - you need to post the comment to a new URL:

        <?php $name=array('name'=>"form$key->postid");
              echo form_open("/welcome/post_comment/$key->postid",$name);

This should do the trick.