Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6806081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:39:59+00:00 2026-05-26T19:39:59+00:00

Okay so, I’m working with CodeIgniter. posts.php is my view that displays all the

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:40:01+00:00Added an answer on May 26, 2026 at 7:40 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, I've looked all over the internet for a good solution to get PHP
Okay so im working on this php image upload system but for some reason
Okay, here's the scenario. I have a utility that processes tons of records, and
Okay, we know that the following two lines are equivalent - (0 == i)
Okay, here's the rundown. I'm developing a video hosting site with PHP and jQuery
Okay, so I'm trying to make a game that uses this algorithm: http://www.codeproject.com/Articles/15573/2D-Polygon-Collision-Detection But
Okay, I've written my first functional PHP extension. It worked but it was a
Okay, this should be really simple, but I have searched all over for the
Okay I do apologise that I'm out of my depth here. I'm not the
Okay, what I currently have is a TabBarController with five tabs. Each of these

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.