AhmedEls
AhmedEls

Reputation: 728

there's way to get $param from view foreach and use it in controller ? Codeigniter

I'm using codeigniter 3.0.3

And i'm facing problem in more than one section in my project

For example in my view there's a foreach loop to display posts

I want to do another foreach for comments by post-id in the first loop but i can't because post_id is in view inside foreach loop and i can't passing parameters like

$data['postcomments'] = $this->model->function($post_id);

Here's my view

            <?php if($allposts): ?>
                <?php foreach ($allposts as $post): ?>
                    <div class="panel panel-default">
                        <div class="panel-body">
                            <div class="col-md-12 text-muted"><small><?=$post->post_text?></small></div>
                            <div class="col-md-12 text-muted"><small><?=$post->post_id?></small></div>
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>

It's possible or how to do foreach for comments in each post by post_id in comments table

Comments_table columns

+-------------+---------+---------+
|commenter_id | comment | post_id |
+-------------+---------+---------+

Upvotes: 2

Views: 193

Answers (3)

AhmedEls
AhmedEls

Reputation: 728

Here's all my code

View :

            <?php if($allposts): ?>
                <?php foreach ($allposts as $post): ?>
                    <div class="panel panel-default">
                        <div class="panel-body">
                            <div class="col-md-12" style="padding: 0;">
                                <img class="img-rounded pull-left" src="<? echo base_url($post->image) ?>" width=30px height=30px>
                                <div style="margin-top: 5px;">
                                    <a href="<? echo base_url('home?p=Profile&u='.$post->post_author) ?>"><small style="margin-left: 5px;"><?=$post->post_author?></small></a>
                                    <span style="font-size: 75%;" class="text-muted pull-right"><span class="fa fa-clock-o"></span>
                                    <?php $post_date = $post->post_date; $now = time(); $units = 1; ?>
                                        <? echo timespan($post_date, $now, $units) ?>
                                            ago</span>
                                </div>
                            </div>
                            <div class="clear"></div>
                            <hr class="myown">
                            <div class="col-md-12 text-muted"><small><?=$post->post_text?></small></div>
                            <div class="clear"></div>
                            <hr class="myown">
                            <div class="btn-group">
                                <?php if($this->users_model->liked($post->pid)): ?>
                                    <a class="btn btn-default btn-sm disabled"><span class="fa fa-check"></span> Liked</a>
                                    <?php else: ?>
                                        <a class="btn btn-default btn-sm like-btn" post-id="<?=$post->pid?>"><span data-toggle-tooltip="tooltip" data-placement="top" title="Like" class="fa fa-thumbs-up fa-lg"></span></a>
                                        <?php endif; ?>
                                            <a class="btn btn-default btn-sm" data-toggle="collapse" data-target="#<?=$post->pid?>Comment"><span data-toggle-tooltip="tooltip" data-placement="top" title="Comment" class="fa fa-comment fa-lg"></span></a>
                                            <a class="btn btn-default btn-sm"><span data-toggle-tooltip="tooltip" data-placement="top" title="Share" class="fa fa-share fa-lg"></span></a>
                            </div>
                            <div class="pull-right-custom text-muted"><small><?=$post->post_likes?> Like - <?=$post->post_comments?> Comment</small></div>
                            <div class="clear"></div>
                            <div id="<?=$post->pid?>Comment" class="collapse">
                                <br>
                                <form name="addcommentajax" id="addcommentajax" post-id="<?=$post->pid?>" class="form-horizontal" role="form" method="POST">
                                    <div class="input-group">
                                        <input type="text" id="comment-text" class="form-control input-sm" placeholder="Add Comment">
                                        <span class="input-group-btn">
                        <button type="submit" class="btn btn-sm btn-default">Publish</button>
                    </span>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                    <?php endforeach; ?>
                        <?php endif; ?>

Controller :

function index () {
            $data['allposts'] = $this->users_model->getAllPosts();
            $data['postcomments'] = $this->users_model->getAllComments();
            $data['friendsrequests'] = $this->users_model->own_friends_request($this->session->userdata('id'));
            $this->load->view('includes/header', $data);
            $this->load->view('subpages/' . $page, $data);
            $this->load->view('includes/footer', $data);
}

Users_model :

function getAllPosts() {


        $this->db->order_by('post_date DESC');
        $this->db->select('*');
        $this->db->from('users_posts');
        $this->db->join('confirmed_users', 'users_posts.author_id = confirmed_users.uid');
        $query = $this->db->get();

        if($query->num_rows() > 0) {
            return $query->result();
        } else {
            return false;
        }
    }

    function getAllComments() {

        $this->db->order_by('comment_date DESC');
        $this->db->select('*');
        $this->db->from('comments_table');
        $this->db->join('confirmed_users', 'comments_table.commenter_id = confirmed_users.uid');
        $query = $this->db->get();

        if($query->num_rows() > 0) {
            return $query->result();
        } else {
            return false;
        }

    }

Upvotes: 0

AhmedEls
AhmedEls

Reputation: 728

Here is the controller

$data['postcomments'] = $this->users_model->getAllComments();

I just passing all comments to view

And here's the model to get all comments

function getAllComments() {

    $this->db->order_by('comment_date DESC');
    $this->db->select('*');
    $this->db->from('comments_table');
    $this->db->join('confirmed_users', 'comments_table.commenter_id = confirmed_users.uid');
    $query = $this->db->get();

    if($query->num_rows() > 0) {
        return $query->result();
    } else {
        return false;
    }

}

here's my foreach loop to display posts

                    <?php if($allposts): ?>
                        <?php foreach ($allposts as $post): ?>
                            <div class="panel panel-default">
                                <div class="panel-body">
                                    <div class="col-md-12" style="padding: 0;">
                                        <img class="img-rounded pull-left" src="<? echo base_url($post->image) ?>" width=30px height=30px>
                                        <div style="margin-top: 5px;">
                                            <a href="<? echo base_url('home?p=Profile&u='.$post->post_author) ?>"><small style="margin-left: 5px;"><?=$post->post_author?></small></a>
                                            <span style="font-size: 75%;" class="text-muted pull-right"><span class="fa fa-clock-o"></span>
                                            <?php $post_date = $post->post_date; $now = time(); $units = 1; ?>
                                                <? echo timespan($post_date, $now, $units) ?>
                                                    ago</span>
                                        </div>
                                    </div>
                                    <div class="clear"></div>
                                    <hr class="myown">
                                    <div class="col-md-12 text-muted"><small><?=$post->post_text?></small></div>
                                    <div class="clear"></div>
                                    <hr class="myown">
                                    <div class="btn-group">
                                        <?php if($this->users_model->liked($post->pid)): ?>
                                            <a class="btn btn-default btn-sm disabled"><span class="fa fa-check"></span> Liked</a>
                                            <?php else: ?>
                                                <a class="btn btn-default btn-sm like-btn" post-id="<?=$post->pid?>"><span data-toggle-tooltip="tooltip" data-placement="top" title="Like" class="fa fa-thumbs-up fa-lg"></span></a>
                                                <?php endif; ?>
                                                    <a class="btn btn-default btn-sm" data-toggle="collapse" data-target="#<?=$post->pid?>Comment"><span data-toggle-tooltip="tooltip" data-placement="top" title="Comment" class="fa fa-comment fa-lg"></span></a>
                                                    <a class="btn btn-default btn-sm"><span data-toggle-tooltip="tooltip" data-placement="top" title="Share" class="fa fa-share fa-lg"></span></a>
                                    </div>
                                    <div class="pull-right-custom text-muted"><small><?=$post->post_likes?> Like - <?=$post->post_comments?> Comment</small></div>
                                    <div class="clear"></div>
                                    <div id="<?=$post->pid?>Comment" class="collapse">
                                        <br>
                                        <form name="addcommentajax" id="addcommentajax" post-id="<?=$post->pid?>" class="form-horizontal" role="form" method="POST">
                                            <div class="input-group">
                                                <input type="text" id="comment-text" class="form-control input-sm" placeholder="Add Comment">
                                                <span class="input-group-btn">
                                <button type="submit" class="btn btn-sm btn-default">Publish</button>
                            </span>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    <?php endif; ?>

I need to do another foreach inside this loop <?php foreach ($allposts as $post): ?> to display comments while $postcomments->post_id = $post->post_id

Upvotes: 0

Peter
Peter

Reputation: 192

Try:

Controller

public function comments()    {

        $this->load->model('comments_model');

        $data["comments"] = $this->comments_model->getAllComments();

        $this->load->view("post", $data);
    }

Model

public function getAllComments()
   {
       return $this->db->get("comments_table")->result();
   }

View:

<?php foreach($comments as $post) : ?>
<div class="panel panel-default">
    <div class="panel-body">
        <div class="col-md-12 text-muted"><small><?php echo $post->comment?></small></div>
        <div class="col-md-12 text-muted"><small><?php echo $post->postid?></small></div>
    </div>
</div>

Upvotes: 1

Related Questions