2016-09-28 93 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'); 
    } 
}