CodeIgniter:帮助解决此问题

时间:2011-01-08 00:05:03

标签: php codeigniter

我刚开始使用我的第一个CI应用。我有一个显示一些帖子的视图。每个帖子都可以有多个评论,我想显示每个帖子旁边的评论总数。

到目前为止,我的所有数据库调用都在我的控制器中(将改变它)。

function index(){
    $data['query'] = $this->db->get('posts');
    $this->load->view('blog_view', $data);
}

在我看来:

<?php foreach($query->result() as $row): 
      <div class="post-box">
          <p><?php echo $row->body; ?><small>&nbsp;added by <?php echo $row->username; ?> on  <?php echo date ('d/m/Y',strtotime($row->created)); ?>&nbsp;<a href="<?php echo base_url(); ?>blog/comments/<?php echo $row->id; ?>"><img src="<?php echo base_url(); ?>images/comments_icon.png" />&nbsp;0</a></small></p>
      </div>
<?php endforeach; ?>

我想得到评论的总数,其中comment.post_id =当前记录的ID。并将其显示在评论图标旁边。

对此最感谢的任何帮助,

比利

更新

控制器:

function index(){
    //load the model
    $this->load->model('City_model');

    //call the model method
    $data->posts = $this->City_model->get_posts();


    $this->load->view('blog_view', $data);
}

模型(city_model.php):

<?php

class City_model extends Model{

    function get_posts($id = NULL) {

        //define optional id for single post
        //if an id was supplied
        if ( $id != NULL ) {
            $this->db->where('id',$id);
        }

        // execute query
        $query = $this->db->get('posts');

        //make sure results exist
        if($query->num_rows() > 0) {
            $posts = $query->result();
        } else {
            return FALSE;
        }

        //create array for appended (with comments) posts
        $appended_posts_array = array();

        //loop through each post
        foreach ($posts as $post) {

            //get comments associated with the post
            $this->db->where('post_id', $post->id)
            $comments = $this->db->get('comments');

            //if there are comments, add the comments to the post object
            if($comments->num_rows() > 0) {
                $post->comments = $comments;
            }
            else {
                $post->comments = array();
            }

            //rebuild the returned posts with their comments
            $appended_posts_array[] = $post;

        }

        //if post id supplied, only return the single post object
        if ($id != NULL) {
            return $appended_registration_array[0];
        }
        else {
            return $appended_registration_array;
        }
    }
}

1 个答案:

答案 0 :(得分:3)

这是一个示例模型方法,它采用我通常对具有“子”项的项目的方法...

我通常在模型中的多级数组中构建它们,就像这样......

注意:此模型会返回帖子或所有帖子,并附有可通过->comments属性访问的相关评论数组。

function get_posts($id = NULL) {

    //define optional id for single post
    //if an id was supplied
    if ( $id != NULL ) {
        $this->db->where('id',$id);
    }

    // execute query
    $query = $this->db->get('posts');

    //make sure results exist
    if($query->num_rows() > 0) {
        $posts = $query->result();
    } else {
        return FALSE;
    }

    //create array for appended (with comments) posts
    $appended_posts_array = array();

    //loop through each post
    foreach ($posts as $post) {

        //get comments associated with the post
        $this->db->where('post_id', $post->id)
        $comments = $this->db->get('comments');

        //if there are comments, add the comments to the post object
        if($comments->num_rows() > 0) {
            $post->comments = $comments;
        }
        else {
            $post->comments = array();
        }

        //rebuild the returned posts with their comments
        $appended_posts_array[] = $post;

    }

    //if post id supplied, only return the single post object
    if ($id != NULL) {
        return $appended_registration_array[0];
    }
    else {
        return $appended_registration_array;
    }       
}

现在在控制器......

function posts() {

    //load the model
    $this->load->model('model_name');

    //call the model method
    $data->posts = $this->model_name->get_posts();

    //load the view
    $this->load->view('view/file', $data);

}

然后在视图中,您可以使用嵌套的foreach遍历帖子和评论

<? foreach($posts as $post): ?>                //posts foreach start

    <h1><?= $post->title ?></h1>  //post title
    <p><?= $post->body ?></p>     //post body

    <? foreach($post->comments as $comment): ?>     //comments foreach start       
        <h3><?= $comment->author ?></h3>  //comment author
        <p><?= $comment->body ?></h3>     //comment body
    <? endforeach; ?>                               // end comments foreach

<? endforeach; ?>        // end posts foreach

请注意,一旦你构建了我在模型中显示的posts数组,对于每个$ post项,你有一个$ post-&gt;注释,这只是与该帖子相关的一系列注释,所以知道您可以在控制器或视图中调用count($post->comments)以获取与单个帖子相关的评论数量。

因此,关于仅显示计数的问题,在视图中唯一的变化是你不会遍历所有评论,你只需要这样做......

<? foreach($posts as $post): ?>                //posts foreach start

    <h1><?= $post->title ?></h1>             //post title
    <p><?= count($post->comments) ?></p>     //comment count


<? endforeach; ?>        // end posts foreach

编辑: 我将可选参数$id添加到模型方法中,以便您可以根据需要指定单个帖子,方法是将其id传递给方法。这样,可以重复使用相同的模型方法来详细显示单个帖子,同时显示所有注释。