在我的模型中,我有这段代码:
$sql = "SELECT SUM(options.points) as total
FROM responses
JOIN options ON options.option_id = responses.option_id
JOIN questions ON questions.question_id = responses.question_id
WHERE options.question_id = questions.question_id
AND questions.category_id = " . $category_id . "
AND responses.assessment_id = " . $assessment_id;
$results = $this->db->query($sql);
return $results->row()->total;
在我的帮手中代码是:
$points = $nin->reportmodel->getTotalPointsAssessmentByCategory(100, $assessment_id);
当我尝试显示“$ points'没有什么或零。如果我将代码移出模型并将其放入帮助程序,则代码可以正常工作。我错过了什么?
答案 0 :(得分:0)
尝试以下代码。
Your_model.php
class Your_model extends CI_Model {
public function getTotalPointsAssessmentByCategory($category_id, $assessment_id) {
$sql = "SELECT SUM(options.points) as total
FROM responses
JOIN options ON options.option_id = responses.option_id
JOIN questions ON questions.question_id = responses.question_id
WHERE options.question_id = questions.question_id
AND questions.category_id = " . $category_id . "
AND responses.assessment_id = " . $assessment_id;
$query = $this->db->query($sql);
$row = $query->row();
return $row->total;
}
}
在控制器上
public function index() {
$this->load->model('your_model');
$points = $this->your_model->getTotalPointsAssessmentByCategory(100, $assessment_id);
}