从表中获取评论数量

时间:2014-02-17 03:38:52

标签: php codeigniter

我正在努力从表格(评论)中获得一些评论。这是我的数据库结构和我正在使用的一些代码:

评论表

------------------------
| id | bizid | content |
------------------------

商家表

----------------
| id | bizname |
----------------

Class Reviews extends MY_model {
    public function get_biz_id() {
        $query = $this->db->query("select * from biz");
        $id =$query->result();
        return $id;
    }

            public function get_count_reviews($c) {
                    $this->db->select('COUNT(review.id) AS reviewCount');
                    $this->db->join('review', 'review.bizid = biz.id');
                    $this->db->where('bizid', $c);
                    $query = $this->db->get('biz');
                    if ($query->num_rows() < 1) return FALSE;
                    $res = $query->row_array();
                    return $res['reviewCount'];
           }
}

本地控制器

Class Local extends CI_controller {

    public function city() {
        $this->load->model('reviews');                 
        $b = $this->reviews->get_biz_id();
        foreach($b as $bb) {
            $bbb = $bb->id;
            $data['count_reviews'] = $this->reviews->get_count_reviews($bbb);
            $this->load->view('local/index',$data);
        }
        $data['reviews'] = $this->reviews->get_city_reviews($city->id,5,1);
    }

}

这是观点:

<?php
foreach($reviews as $review):               
$biz = getBizById($review->bizid);
?>
<li class="clearfix">
    <div class="span-1">
        <a href="<?=site_url('member/'.$review->username)?>">
            <img class="thumbimg thumb40" src="<?=site_url(avatar($review->uid))?>"/>
        </a>
    </div>

    <div class="span-13">
        <div class="title-name">
            <a  href="<?=site_url('member/'.$review->username)?>"><?php echo ucfirst($review->username)?></a> reviewed
            <a href="<?=site_url('biz/'.$biz->id)?>#review_<?php echo $review->id ?>"><?=$biz->name?></a>
        </div>

        <div class="span-3 rating-wrap rating-<?php echo $review->rating ?>"></div>
        <div><?php echo $count_reviews;  ?></div>

        <p class="clear"><?=wordwrap($review->content,55,"<br>\n")?></p>

    </div>

</li> 
<?php endforeach;?>

我想回复($count_reviews)旁边的评论数量<div class ="span-3">,但由于我还需要回应其他事情,我必须使用foreach(),这就是我的问题开始的地方。

输出

MC唐纳德 评论数量(1)

杰米约翰斯 评论数量(1)

KFC 评论数量(1)

甚至认为只有MC唐纳德有一个评论

由于

1 个答案:

答案 0 :(得分:0)

使用JOIN获取评论次数:

public function get_count_reviews($c) {
        $this->db->select('COUNT(review.id) AS reviewCount');
        $this->db->join('review', 'review.bizid = biz.id');
        $this->db->where('bizid', $c);
        $query = $this->db->get('biz');
        if ($query->num_rows() < 1) return FALSE;
        $res = $query->row_array();
        return $res['reviewCount'];
    }

还可以在下面的控制器中更改城市功能

foreach($b as $bb) {
            $bbb = $bb->id;
            $data['count_reviews'][$bb->id] = $this->reviews->get_count_reviews($bbb);//save all instead of overwriting
            $this->load->view('local/index',$data);
}

view

<div><?php echo (isset($count_reviews[$biz->id]))?$count_reviews[$biz->id]:0;  ?></div>