У меня есть 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_id
questionnaire_id_fk
choices_id_fk
user_id_fk
status
Вот моя модель:
Код: Выделить всё
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;
}
Код: Выделить всё
Unanswered:
Incorrect:
Результаты класса расширяют 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);
}
}
Подробнее здесь: https://stackoverflow.com/questions/315 ... count-rows
Мобильная версия