I am using a PHP framework CodeIgniter to throw together a quick project. I am running into a PHP related issue with one of my loops/arrays and I am looking for a push in the right direction.
PHP Database Call:
public function get_feed_data()
{
$query = $this->db->query('SELECT c.id, c.name, c.age, c.photoName, c.photoNameSmall, c.panelColor , IFNULL(SUM(t.points), '.MAX_POINTS.') as totalPoints
FROM children as c
LEFT JOIN behaviorRatings as r
ON c.id = r.childID
LEFT JOIN behaviorTypes as t
ON r.behaviorID = t.behaviorTypeID
GROUP BY c.id');
return $query->result();
}
My Controller:
$feedData = $this->display_model->get_feed_data();
$output = array();
foreach($feedData as $d)
{
$rating = $this->_current_behavior($d->totalPoints[0]);
$d['rating'] = $rating;
$output[] = $d;
}
$response = array();
$response['timestamp'] = $currentmodif;
$response['children'] = $output;
echo json_encode($response);
The Issue:
The database call returns multiple records of data from the tables without any issues. However, I am trying to add another key/value pair to that array that is calculated through another function.
In the array, I am trying to add the key rating with the value of $rating.
The error I am getting is Cannot use object of type stdClass as array.
I assumed that $feedData was an array that I could have just pushed this value into but that doesn't seem to be the case.
Any thoughts?
$d['rating'] = $rating;to$d->rating = $rating. as the error suggests, the array$feedDataactually a bunch ofstdClassnot another array, so you only need to use it as object. luckily,phpis such a nice language, it lets you to add object properties on a whim.