我做一个类似新浪微博的程序,我想获取我收到的评论。因为评论有别人对我发的微博的评论,也有我去别人微博下面发的评论然后别人回复我的评论,请问怎样获取所有这些评论?现在数据表(评论表)有这样几个自字段 :id(评论id),uid(评论发布者),content(评论内容),wid(评论所属微博id)

解决方案 »

  1.   

    楼主是不是在做后盾微博的那个项目??我当时是这么改的,你可以参考我的代码注释。最后能够取出“我”发的评论和“我”收到的所有评论,代码如下: /**
     * 展示评论列表
     */
    public function comment(){ //重置缓存
    set_msg(session('uid'), 1, true); //取出我发的微博的id
    $my_uid = session('uid');
    $wids = M('weibo')->where(array('uid'=>$my_uid))->field('id')->select(); //取出我参与评论的微博的id
    $comment_db = M('comment');
    $other_wids = $comment_db->where(array('uid'=>$my_uid))->field('wid AS id')->select();

    //合并所有需要取出的微博id
    $wids = array_merge_recursive($wids, $other_wids);
    foreach($wids as $k=>$v){
    $wids[$k] = $v['id'];
    }
    $wids = array_unique($wids); //合并后去重复 //根据wid取出comment表的id,uid,content,time等信息,再把comment表的id取出
    $uids = $comment_db->where(array('wid'=>array('IN', $wids)))->select();
    foreach($uids as $k=>$v){
    $cids[$k] = $v['id'];
    }

    //根据comment表的id取出评论
    $db = D('CommentView');
    $where = array('comment.id'=>array('IN', $cids)); //分页
    $count = $db->where($where)->count();
    import('ORG.Util.Page');
    $page = new Page($count, 10);
    $limit = $page->firstRow . ',' . $page->listRows; $comment = $db->where($where)->order('time DESC')->limit($limit)->select();

    //分配变量到模板
    $assign = array('count'=>$count, 'comment'=>$comment, 'page'=>$page->show());
    $this->assign($assign);

    $this->display();
    }