Here is my model. I’m not sure I’m on the right track, but this is a start. I have a survey form on my website and all of the values are numeric in INT fields. I want to compute the average of each of 10 columns. Rather than computing each result separately, I was wondering if there was a way to use a loop to process the results. So here it is:
function survey_averages() {
$q = array(
'1' => 'q1',
'2' => 'q2',
'3' => 'q3',
'4' => 'q4',
'5' => 'q5',
'6' => 'q6',
'7' => 'q7',
'8' => 'q8',
'9' => 'q9',
'10' => 'q10'
);
for ($i=1; $i<11; $i++) {
$this->db->select_avg($q[$i]);
$query[$i] = $this->db->get('survey');
}
return $query;
}
If I’m on the right track, then how to I get back the results of the array? If I’m on the wrong track, what do I need to change? Also, if I do this in my controller:
foreach($query->result_array() as $row) {
How do I return the rows of my array? PHP is not letting me do $row->1 or $row->’1′.
try
var_dump($query->result_array())