查询mysql的帖子和评论系统

时间:2013-09-10 13:27:17

标签: mysql codeigniter

我正在使用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;
        }

    }

users_table

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

3 个答案:

答案 0 :(得分: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>";
            }
        }
    }
}

答案 1 :(得分: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('table','relation','join type')

答案 2 :(得分: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'];