有办法从foreach中获取$ param并在控制器中使用它吗?笨

时间:2015-12-26 20:52:01

标签: php codeigniter foreach

我正在使用codeigniter 3.0.3

我在项目中的多个部分面临问题

例如,在我看来,有一个foreach循环来显示帖子

我想在第一个循环中通过post-id做另一个foreach的注释,但我不能因为post_id在foreach循环中的视图中而且我不能传递像

这样的参数

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

这是我的观点

            <?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; ?>

可以或如何通过post_id在评论表

中为每篇帖子做评论

Comments_table专栏

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

3 个答案:

答案 0 :(得分:1)

尝试:

<强>控制器

public function comments()    {

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

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

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

<强>模型

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

查看:

<?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>

答案 1 :(得分:0)

这是控制器

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

我只是将所有评论传递给视图

这是获得所有评论的模型

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;
    }

}

这是我的foreach循环显示帖子

                    <?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; ?>

我需要在此循环<?php foreach ($allposts as $post): ?>中再做一次foreach,以便在$postcomments->post_id = $post->post_id

时显示评论

答案 2 :(得分:0)

这是我的所有代码

查看:

            <?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; ?>

控制器:

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;
        }

    }