2015-07-21 86 views
0

如何從數據庫中統計行數? 我有3個表,即:Codeigniter count rows

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個問題,但它在未答覆函數返回。對於不正確函數返回

+0

您不包括表結構和我們不明白'我回答了這些2個問題,但在未答覆的函數返回2。對於不正確的功能,它返回3'。你能否至少提供每個表的列名? – ekad

回答

0

我只是我自己得到了解決。

這裏是我的解決方案:

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

你應該發佈查詢,然後你可以接受你自己的答案。 – gonzalon

0

你嘗試白衣一個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

+0

我還沒試過。我可能會嘗試。謝謝。 –

+0

感謝您的回答。它工作正常。但是我如何計算那些還沒有回答的行,正如我在文章中所說的那樣。 –

+0

您修復了錯誤的計數?爲什麼沒有解決左路加入問題?目前你的代碼是什麼?@JohnLopeValmoria – gonzalon