1

我正在使用 codeigniter 进行发布和评论系统。我做了所有的查询,但我不知道如何获取表 post_comment 中的评论。这是我对分享所有帖子的查询。感谢您的帮助。

function get_post_profile($user_id,$limit) {
        $this->db->select('post_user.user_id,post_user.id_post_shared,post_shared.post_text,users.name,users.surname,users.id');
        $this->db->join('post_shared', 'post_shared.id_post = post_user.id_post_shared');
        $this->db->join('users','users.id = post_user.user_id');
        $this->db->where('post_user.user_id',$user_id);
        $this->db->order_by('post_user.id_post_shared','DESC');
        $this->db->limit($limit);
        $query = $this->db->get('post_user');

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

    }

用户表

id | name | surname |
1    jhon    Smith
2    Sally   Dunk

post_user 表

id_post_shared | user_id |
      1             1

post_share 表

id_post | post_text
   1      Hello guys

post_comment 表

comment_text | id_post | id_user
   Hello!        1          2
4

3 回答 3

2

尝试并回复:

$this->select('post_comment.comment_text, post_comment.id_post, post_comment.id_user')->join('post_share', 'post_share.id_post = post_comment.id_post')->join('users', 'users.id = post_comment.id_user')->get('post_comment');

编辑

function get_comments(){
    $data   = array();
    $posts  = array();
    $posts  = $this->db->select('id_post as post_id, post_text', false)->order_by('id_post', 'desc')->get('post_share', 10)->result_array();   #get first 10 posts
    if( is_array( $posts ) && count( $posts ) > 0 ){
        foreach( $posts as $key=>$each ){
            ## gather the comments for the posts ###
            $comments   = array();
            $comments   = $this->db->select('comment_text, id_user')->where('id_post', $each['post_id'])->get('post_comment')->result_array();
            if( is_array( $comments ) && count( $comments ) ){
                $posts[$key]['comments']    = $comments;
            }
        }
    }
    return $posts;
}

编辑 1

if( isset( $posts ) && is_array( $posts ) && count( $posts ) > 0 ){
    foreach( $posts as $key=>$each ){
        echo "Post id :".$each['post_id']." Post txt: ".$each['post_text']."<br>";
        if( isset( $each['comments'] ) && is_array( $each['comments'] ) && count( $each['comments'] ) ){
            foreach( $each['comments'] as $subKey=>$subEach ){
                echo "Comment Txt :".$subEach['comment_text']."<br>";
            }
        }
    }
}
于 2013-09-10T13:35:36.363 回答
0

添加LEFT JOIN到您的 post_comment

$this->db->select('post_user.user_id,post_user.id_post_shared,
post_shared.post_text,users.name,users.surname,users.id,post_comment.comment_text');
        $this->db->join('post_shared', 'post_shared.id_post = post_user.id_post_shared');
        $this->db->join('users','users.id = post_user.user_id');
        $this->db->join('post_comment ','users.id = post_comment .id_user','LEFT');
        $this->db->where('post_user.user_id',$user_id);
        $this->db->order_by('post_user.id_post_shared','DESC');
        $this->db->limit($limit);
        $query = $this->db->get('post_user');

调用中的第三个参数是指定join的类型join('table','relation','join type')

于 2013-09-10T13:37:27.627 回答
0

试试这个:

$sql = "SELECT a.*,b.id,b.name,b.surname,c.id_post_shared,c.user_id,d.id_post,d.post_text            
        FROM post_comment a, users_table b, post_user c,post_share d 
        WHERE b.id = c.user_id 
        AND b.id = $user_id 
        AND c.id_post_shared = d.id_post 
        AND d.id_post = a.id_post";

$result = $this->db->query($sql)->result_array();

echo $result[0]['comment_text'];
于 2013-09-10T14:22:50.490 回答