1

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.

3
  • 2
    your function is returning an array return $res->result_array(); And $average_score['avg'] is an array that why you are getting this error Commented Jan 7, 2016 at 13:28
  • print_r($average_score) and check its value Commented Jan 7, 2016 at 13:30
  • instead of this return false; pass return array(); and check isset() if necessary Commented Jan 7, 2016 at 13:32

4 Answers 4

5

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

Sign up to request clarification or add additional context in comments.

6 Comments

the only problem is that I need the data from quiz and score to be passed to the view
I did like you said and there is no error anymore it's just the avg doesn't display even though the scores are in the database
@Nynaeve with my code you have only 3 variables $quiz, $average_score, $score
I have in my table scores that the previous users received and I want to display the avg score of all of them - that's the plan and the variables you have worked (two of them and but the average_score it's empty for some reason )
I figured out why I had a problem with the query, your code was good btw, thanks so much for the help
|
3
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

4 Comments

I don't get the error anymore but no score is displayed in the view even though it is in the table
Is it possible to you to make queries from phpmyadmin or something?
select avg(score) from userScoreQuiz where id=**yourid**
I did that and I noticed a mistake in the query and that's why no result was showing in the view, thanks for the help
0

replace the

echo $average_score['avg'];

to

print_r($average_score['avg'];

because that not one variable, that is set of array

3 Comments

it will print array not the desire result.
But we don't know about what desire result, then only I give that solution. If user see the array set, then he get that solution.
this is obvious the average score Avg. score on all previous attempts: <?php echo $average_score['avg'] ?>
0

codeigniter result_array() returns data as array. And array index starts with 0 index.

"$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.

if you want to avoid extra array index then return results using row() instead on result_array().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.