Codeigniter中的数组到字符串的转换错误 [英] Array to string conversion error in Codeigniter
本文介绍了Codeigniter中的数组到字符串的转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从数据库向视图显示平均结果,但我不断收到此错误:
I'm trying to display an average result from the database to the view but I keep getting this error:
遇到PHP错误
严重性:通知
消息:数组到字符串的转换
Message: Array to string conversion
文件名:views/resultview.php
Filename: views/resultview.php
行号:38
这是控制器中的代码:
$average['avg'] = $this->quiz->getAverage($quizid);
$this->load->view('resultview',array('quiz' => $quiz,
'score' => $score,
'average_score' => $average));
该模型的功能如下:
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>
我无法找出原因.
谢谢您的帮助.
推荐答案
您正在进行的代码过多,这是一种优雅的解决方案:
That's too much of coding you got going on, Here is an Elegant solution:
function getAverage($quiz)
{
//get percentage from the database
$query = $this->db->select('AVG(score) as average_score')->from('userScoreQuiz')->where('id', $quiz)->get();
return $query->row()->average_score;
}
您的看法
$data['quiz'] = //fill this area
$data['average_score'] = $this->quiz->getAverage($quizid);
$data['score'] = //fill this area
$this->load->view('resultview', $data);
它们将可以作为$quiz, $average_score, $score
这篇关于Codeigniter中的数组到字符串的转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文