Akshat Tyagi Akshat Tyagi - 5 months ago 170
SQL Question

Array to String Conversion Error CodeIgniter using select_avg() function

I've started learning the CodeIgniter framework recently and I'm developing this code which calculates the average feedback. Basically, a number of entries are there in the 'SERVICES' column and I want to return the average of these values and display it in a view as the average feedback. When I run this code, I get the following PHP error:


Severity: Notice

Message: Array to string conversion


Please tell me where I'm going wrong. Ibelieve that it's because my data passing is worng. How do I sort this problem in order to return the average value in the view?

Controller Code

function _admin_load_Home(){
$data_admin=array(
'avg_feedback' => $this->feedback->getAverage()
);
$this->load->view('header',$data_admin);
}


Model Code

function getAverage(){
$avg_data = array(
'SERVICE' => $this->db->select_avg('SERVICE')->from('feedback')- >get()->result()
);
return $avg_data;
}


View Code

<tr>
<th><?php echo $avg_feedback['SERVICE']; ?></th>
</tr>


Note: 'SERVICE' is just one of the many 'feedback' table columns.

Answer

In view get you array as

echo $data['SERVICE'][0]->SERVICE;
Comments