Akshat Tyagi Akshat Tyagi - 1 year ago 392
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(){
'avg_feedback' => $this->feedback->getAverage()

Model Code

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

View Code

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

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

Answer Source

In view get you array as

echo $data['SERVICE'][0]->SERVICE;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download