【问题标题】:Codeigniter count rowsCodeigniter 计数行
【发布时间】:2015-07-21 01:44:25
【问题描述】:

如何计算数据库中的行数? 我有3张桌子,即:

questionnaire
choices
answer_sheet

这是表结构:

问卷调查

questionnaire_id
type_id_fk
question
answer
pg

选择

choices_id
questionnaire_id_fk
type_id_fk
choices
pg

answer_sheet

answer_sheet_id
questionnaire_id_fk
choices_id_fk
user_id_fk
status

我的问题是,我如何计算unanswered 问题和incorrect 问题?

这是我的模型:

function countUnanswered($userID, $type) {
        $this->db->select('*');
        $this->db->from('questionnaire q');
        $this->db->join('answer_sheet a', 'q.questionnaire_id=a.questionnaire_id_fk');
        $this->db->where('q.type_id_fk', $type);
        $this->db->where('a.user_id_fk', $userID);
        $query = $this->db->get();
        $rowcount = $query->num_rows();
        return $rowcount;
    }

function countIncorrect($userID, $type) {
        $this->db->select('*');
        $this->db->from('answer_sheet a');
        $this->db->join('questionnaire q', 'q.answer!=a.choices_id_fk', 'LEFT');
        $this->db->where('q.type_id_fk', $type);
        $this->db->where('a.user_id_fk', $userID);
        $query = $this->db->get();
        $rowcount = $query->num_rows();
        return $rowcount;
    }

这是我的看法:

<p><label>Unanswered:</label> <span class="badge"><?php echo $countUnanwered = $this->Mresults->countUnanswered('1'); ?></span></p>
<p><label>Incorrect:</label> <span class="badge"><?php echo $countIncorrect = $this->Mresults->countIncorrect($userID, '1'); ?></span></p>

这是我的控制器

类结果扩展 CI_Controller {

   public function view($page = 'results') {
        if (!file_exists(APPPATH . '/views/pages/results/' . $page . '.php')) {
            // Whoops, we don't have a page for that!
            show_404();
        }
        $data['title'] = ucfirst($page) . ' | TOEFL Practice Test'; // Capitalize the first letter
        $data['page'] = $page;
        $this->load->view('template/header', $data);

        if (isset($this->session->userdata['logged_in'])) {
            $data['userID'] = ($this->session->userdata['logged_in']['userID']);
            $data['username'] = ($this->session->userdata['logged_in']['username']);
            $data['email'] = ($this->session->userdata['logged_in']['email']);

            $userID = $this->session->userdata['logged_in']['userID'];

            $this->load->view('pages/results/' . $page, $data);
        } else {
            $this->load->view('pages/user/login_form', $data);
        }

        $this->load->view('template/footer', $data);
    }

}

我在 问卷调查表 中插入了 2 个问题,我回答了这 2 个问题,但它在 Unanswered 函数中返回 2。对于 incorrect 函数,它返回 3

【问题讨论】:

  • 您没有包含表格结构,我们不理解I answered those 2 questions but it returns 2 in Unanswered function. For the incorrect function it returns 3。你能至少提供每个表的列名吗?

标签: php codeigniter activerecord


【解决方案1】:

你尝试过 COUNT 函数吗?

例子:

 $this->db->select('user_id, COUNT(user_id) as total');
 $this->db->group_by('user_id'); 
 $this->db->order_by('total', 'desc'); 
 $this->db->get('tablename', 10);

你可以多看看here

【讨论】:

  • 我没试过。我可能会尝试一下。谢谢。
  • 感谢您的回答。它工作正常。但是,我如何计算尚未回答的行,正如我在帖子中所说的那样。
  • 你修正了不正确的计数?为什么在未回答的情况下左连接不工作?你现在的代码是什么? @JohnLopeValmoria
【解决方案2】:

我自己刚刚得到了解决方案。

这是我的解决方案:

<?php
   $total = $this->Mresults->totalQuestions('1');
   $answered = $this->Mresults->countAnswered($userID, '1');
   $unAnswered = $total-$answered;
   echo $unAnswered;
?>

【讨论】:

  • 您应该发布查询,然后您可以接受自己的答案。
猜你喜欢
  • 2015-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多