Codeigniter 带有联接的计数仍不显示正确的计数

Codeigniter 带有联接的计数仍不显示正确的计数,codeigniter,Codeigniter,我试图做的是创建一个列表,列出这个线程有多少回复。我可以很好地计算回复的数量 但我希望能够做的是计算用户线程也 这就是输出的样子。它只从我的replys表中计数,似乎无法从我的join中计数 Username Post demo 2 admin 1 应该像 Username Post demo 3 <-- Because the user has asked the Question / "thread" and has repl

我试图做的是创建一个列表,列出这个线程有多少回复。我可以很好地计算回复的数量

但我希望能够做的是计算用户线程也

这就是输出的样子。它只从我的replys表中计数,似乎无法从我的join中计数

Username    Post
demo         2
admin        1
应该像

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

<?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;
        }
    }
}   

对2个表用户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;
    }
}

Do sum 2表用户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;
    }
}

我似乎已经让它工作了,主要是现在有更多的测试要做

我所要做的就是创建函数来计算一个回复数和一个线程数

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');
}
然后将它们组合起来,使用plus
+
然后返回

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);
}
然后在数组本身中使用
总数和线程($user\u id,$thread\u 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');
    }
}

我似乎已经让它工作了,现在大部分时间都需要做更多的测试

我所要做的就是创建函数来计算一个回复数和一个线程数

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');
}
然后将它们组合起来,使用plus
+
然后返回

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);
}
然后在数组本身中使用
总数和线程($user\u id,$thread\u 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');
    }
}

您能提供样本数据和表格吗columns@NewbeeDev按线程添加tablesDo组的映像_id@GulmuhammadAkbari无法工作。您能否提供样本数据和表格columns@NewbeeDev按线程添加tablesDo组的映像_id@GulmuhammadAkbari现在不是只返回1行吗?可能是1行还是什么?我在使用时有点麻烦连接但是找到了另一种方法添加了我的答案谢谢你的时间。现在只返回1可能是1行或什么?我使用连接有点麻烦,但是找到了另一种方法添加了我的答案谢谢你的时间。