我要做的是创建一个列表,列出该线程有多少回复。我可以很好地计算答复的数量。
但我想要做的是计算用户线程。
这就是输出看起来的样子。它只从我的回复表中计算它似乎无法从我的连接计算它。
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;
}
}
}
主题表
回复表
答案 0 :(得分:0)
总结2个表user_id列
reason: 'Provided bucket: PROJECT1.appspot.com does not match bucket specified in FIRApp configuration: PROJECT2.appspot.com
答案 1 :(得分: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'])
);
}
}
现在输出正确
控制器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');
}
}