2016-09-29 91 views
0

我具有低於結合的結果爲一個,如果多個

public function get_posts() { 
    $this->db->select('p.post_id, p.reply_id, p.user_id, u.username'); 
    $this->db->from('post as p'); 
    $this->db->join('user as u', 'u.user_id = p.user_id'); 
    $this->db->where('p.post_id', $this->input->get('post_id')); 
    $this->db->or_where('p.reply_id', $this->input->get('post_id')); 
    // $this->db->group_by('p.user_id'); 
    $query = $this->db->get(); 
    return $query->result_array(); 
} 

結果輸出這兩個功能

SELECT * FROM `post` WHERE `post_id` = '1' AND `reply_id` = '0' AND `user_id` = '2' 
SELECT * FROM `post` WHERE `post_id` = '3' AND `reply_id` = '1' AND `user_id` = '1' 
SELECT * FROM `post` WHERE `post_id` = '5' AND `reply_id` = '1' AND `user_id` = '1' 

帖子總數功能

public function total_posts_by_user($user_id, $reply_id, $post_id) { 
    $this->db->from('post'); 
    $this->db->where('post_id', $post_id); 
    $this->db->where('reply_id', $reply_id); 
    $this->db->where('user_id', $user_id); 
    // $this->db->group_by('user_id'); 
    $query = $this->db->get(); 

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

的total_posts_by用戶得到所示柱的數目in image below

enter image description here

問題正如你可以看到有2行管理信息顯示。但我 希望他們合併,所以它只會說管行和2 職位。

我一直在使用上get_post但DOS GROUP_BY()無法正常工作

控制器

<?php 

class Who_replied extends MX_Controller { 

    public function __construct() { 
     parent::__construct(); 
    } 

    public function index() { 
     $data['posts'] = array(); 

     $results = $this->get_posts(); 

     foreach ($results as $result) { 
      $data['posts'][] = array(
       'username' => $result['username'], 
       'total' => $this->total_posts_by_user($result['user_id'], $result['reply_id'], $result['post_id']) 
      ); 

      $this->total_posts_by_user($result['user_id'], $result['reply_id'], $result['post_id']); 
      echo $this->db->last_query() . '</br>'; 
     } 

     $data['total_posts'] = $this->total_posts(); 

     return $this->load->view('default/template/forum/categories/who_replied_view', $data); 

    } 
} 
+0

有你只在第一種方法'get_posts試圖'GROUP_BY( 'p.post_id')' ()'?此外,你可以嘗試'DISTINCT' post_id –

+0

@RejoanulAlam只是還是一樣。 – user4419336

+0

請檢查我的回答 –

回答

0

使用left,因爲目前它正在使用內部連接在第一種方法加入試過。之後使用羣組

public function get_posts() { 
    $this->db->select('p.post_id, p.reply_id, p.user_id, u.username'); 
    $this->db->from('post as p'); 
    $this->db->join('user as u', 'u.user_id = p.user_id','left'); 
    $this->db->where('p.post_id', $this->input->get('post_id')); 
    $this->db->or_where('p.reply_id', $this->input->get('post_id')); 
    $this->db->group_by('p.post_id'); 
    $query = $this->db->get(); 
    return $query->result_array(); 
} 

並嘗試。如果仍然面臨同樣的問題,請仔細檢查是否有相同的TWO用戶名admin

**您是否嘗試過DISCTINCT? 希望它將使用COUNT(p.user_id)佔總幫助

+0

找到解決方案。謝謝你的時間 – user4419336

0

我發現我的解決方案從here

public function get_posts() { 
$this->db->select('p.post_id, p.reply_id, p.user_id, u.username COUNT(p.user_id) AS total'); 
$this->db->from('post as p'); 
$this->db->join('user as u', 'u.user_id = p.user_id'); 
$this->db->where('p.post_id', $this->input->get('post_id')); 
$this->db->or_where('p.reply_id', $this->input->get('post_id')); 
$this->db->group_by('p.user_id'); 
$query = $this->db->get(); 
return $query->result_array(); 
}