I'm trying to display an average result from the database to the view but I keep getting this error:
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: views/resultview.php
Line Number: 38
Here is the code from the Controller:
$average['avg'] = $this->quiz->getAverage($quizid);
$this->load->view('resultview',array('quiz' => $quiz,
'score' => $score,
'average_score' => $average));
The function from the model is the following:
function getAverage($quiz)
{
//get percentage from the database
$this->db->select_avg('score');
$this->db->where('id', $quiz);
$res = $this->db->get('userScoreQuiz');
if ($res->num_rows() != 1) {
// there should only be one row - anything else is an error
return false;
}
return $res->result_array();
}
and the code from the view it is:
<h4> Avg. score on all previous attempts: <?php echo $average_score['avg'] ?> %</h4>
I can't seam to find out why it does this.
Thank you for your help guys.