php Codeigniter中的数组到字符串转换错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34656326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Array to string conversion error in Codeigniter
提问by Nynaeve
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
遇到 PHP 错误
Severity: Notice
严重性:注意
Message: Array to string conversion
消息:数组到字符串的转换
Filename: views/resultview.php
文件名:views/resultview.php
Line Number: 38
行号: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.
谢谢你们的帮助。
回答by Zahur Sh
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->row()->score;
}
from docs:
来自文档:
$this->db->select_avg('age');
$query = $this->db->get('members'); // Produces: SELECT AVG(age) as age FROM members
回答by Ilanus
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;
}
For your view
供您查看
$data['quiz'] = //fill this area
$data['average_score'] = $this->quiz->getAverage($quizid);
$data['score'] = //fill this area
$this->load->view('resultview', $data);
And they will be accessable as $quiz, $average_score, $score
他们将可以作为 $quiz, $average_score, $score
回答by Feroz
codeigniter result_array() returns data as array. And array index starts with 0 index.
codeigniter result_array() 将数据作为数组返回。数组索引从 0 索引开始。
"$average_score['avg']" this should be undefined index. Try to access result array as $average_score['avg'][0]['score'] if you don't want to change in your function.
"$average_score['avg']" 这应该是未定义的索引。如果您不想更改函数,请尝试将结果数组作为 $average_score['avg'][0]['score'] 访问。
if you want to avoid extra array index then return results using row() instead on result_array().
如果你想避免额外的数组索引,那么使用 row() 而不是 result_array() 返回结果。
回答by msvairam
replace the
更换
echo $average_score['avg'];
to
到
print_r($average_score['avg'];
because that not one variable, that is set of array
因为那不是一个变量,那是一组数组