无法从Codeigniter中的Model获得返回值

时间:2016-03-04 15:13:08

标签: php mysql codeigniter

在我的模型中,我有这段代码:

$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'没有什么或零。如果我将代码移出模型并将其放入帮助程序,则代码可以正常工作。我错过了什么?

1 个答案:

答案 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);
}