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');

呼び出しの 3 番目のパラメーターは、結合のタイプを指定することです。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 に答える