2016-09-28 164 views
1

我想要做的是創建一個該線程有多少回覆的列表。我可以計算好答覆的數量。加入計數仍然沒有顯示正確的計數

但我想能做的是計算用戶線程也。

這就是看起來像什麼。它只是從我的答覆表中統計它似乎無法讓它從我的連接算起。

Username Post 
demo   2 
admin  1 

應該出來放像

Username Post 
demo   3 <-- Because the user has asked the Question/"thread" and has replied twice 
admin  1 

問題如何確保它可以指望從join()方法的線程表中的用戶ID?

模型函數

public function number_of_replies($user_id, $thread_id) { 
    $this->db->select('*'); 
    $this->db->from('reply'); 
    $this->db->join('thread', 'thread.user_id = reply.user_id', 'left'); 
    $this->db->where('reply.user_id', $user_id); 
    $query = $this->db->get(); 

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

控制器

<?php 

class Who_replied extends MX_Controller { 

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

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

     $data['thread_id'] = $thread_id; 

     $results = $this->get_users_who_replied($thread_id); 

     if (isset($results)) { 

      foreach ($results as $result) { 
       $data['users'][] = array(
        'user_id' => $result['user_id'], 
        'username' => $result['username'], 
        'total' => $this->number_of_replies($result['user_id'], $thread_id), 
       ); 
      } 

     } 

     $data['total_posts'] = ''; 

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

    } 

    public function get_users_who_replied($thread_id) { 
     $this->db->select('user.username, user.user_id, reply.thread_id'); 
     $this->db->distinct(); 
     $this->db->from('reply'); 
     $this->db->join('user', 'user.user_id = reply.user_id', 'left'); 
     $this->db->join('thread', 'thread.user_id = user.user_id', 'left'); 
     $this->db->where('reply.thread_id', $thread_id); 
     $this->db->order_by('thread.user_id', 'desc'); 
     $query = $this->db->get(); 

     if ($query->num_rows() > 0) { 

      return $query->result_array(); 

     } 
    } 

    public function number_of_replies($user_id, $thread_id) { 
     $this->db->select('*'); 
     $this->db->from('reply'); 
     $this->db->join('thread', 'thread.user_id = reply.user_id', 'left'); 
     $this->db->where('reply.user_id', $user_id); 
     $query = $this->db->get(); 

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

線程表

enter image description here

回覆表

enter image description here

+0

你能提供樣本數據和表格列 – Beginner

+0

@NewbeeDev添加表格圖像 – user4419336

+0

做group by thread_id –

回答

0

待辦事項和2代表的user_id列

public function number_of_replies($user_id, $thread_id) { 
    $this->db->select('t1.*, count(t2.user_id)+count(t1.user_id) AS total_posts'); 
    $this->db->from('reply AS t1'); 
    $this->db->join('thread AS t2', 't2.user_id = t1.user_id', 'left'); 
    $this->db->where('t1.user_id', $user_id); 
    $query = $this->db->get(); 

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

現在只返回1 – user4419336

+0

也許它會是1行或什麼? –

+0

我在使用連接時遇到了一些麻煩,但發現另一種方式已經添加了我的答案,謝謝你的時間。 – user4419336

0

我似乎已經得到它的工作主要是現在更多的位測試做

我不得不做的是創建爲函數計算一個答覆和一個線程

public function replies_by_users($user_id, $thread_id) { 
    $this->db->where('user_id', $user_id); 
    $this->db->where('thread_id', $thread_id); 
    return $this->db->count_all_results('reply'); 
} 

public function thread_by_user($user_id, $thread_id) { 
    $this->db->where('user_id', $user_id); 
    $this->db->where('thread_id', $thread_id); 
    return $this->db->count_all_results('thread'); 
} 

然後結合他們和使用加+,然後返回它

public function total_replies_and_thread($user_id, $thread_id) { 
    return $this->replies_by_users($user_id, $thread_id) + $this->thread_by_user($user_id, $thread_id); 
} 

然後用the total_replies_and_thread($user_id, $thread_id)在陣列本身就像

$results = $this->get_replies($thread_id); 

    if ($results) { 
     foreach ($results as $result) { 
      $data['users'][] = array(
       'reply_id' => $result['reply_id'], 
       'user_id' => $result['user_id'], 
       'thread_id' => $result['thread_id'], 
       'username' => $result['username'], 
       'total' => $this->total_replies_and_thread($result['user_id'], $result['thread_id']) 
      ); 
     } 
    } 

出把現在正確

enter image description here

控制器HMVC

<?php 

class Who_replied extends MX_Controller { 

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

    public function index($user_id, $thread_id) { 
     $data['users'] = array(); 

     $results = $this->get_replies($thread_id); 

     if ($results) { 
      foreach ($results as $result) { 
       $data['users'][] = array(
        'reply_id' => $result['reply_id'], 
        'user_id' => $result['user_id'], 
        'thread_id' => $result['thread_id'], 
        'username' => $result['username'], 
        'total' => $this->total_replies_and_thread($result['user_id'], $result['thread_id']) 
       ); 
      } 
     } 

     $data['user_id'] = $user_id; 

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

    } 

    // Model code is just on here for testing purpose you should all ways create a model for it self. 

    public function get_replies($thread_id) { 
     $this->db->select('reply.*, user.username'); 
     $this->db->from('reply'); 
     $this->db->join('user', 'user.user_id = reply.user_id'); 
     $this->db->where('thread_id', $thread_id); 
     $this->db->group_by('user_id'); 
     $this->db->order_by('thread_id', 'desc'); 
     $query = $this->db->get(); 

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

    public function total_replies_and_thread($user_id, $thread_id) { 
     return $this->replies_by_users($user_id, $thread_id) + $this->thread_by_user($user_id, $thread_id); 
    } 

    public function replies_by_users($user_id, $thread_id) { 
     $this->db->where('user_id', $user_id); 
     $this->db->where('thread_id', $thread_id); 
     return $this->db->count_all_results('reply'); 
    } 

    public function thread_by_user($user_id, $thread_id) { 
     $this->db->where('user_id', $user_id); 
     $this->db->where('thread_id', $thread_id); 
     return $this->db->count_all_results('thread'); 
    } 
}