I have multiple fivestar rating fields that I want to calculate using the computed fields module. I was able to display the average using PHP but I need to have the computed fields module input my average into the DB for the node.
$total =
$content['field_business_q1'][0]['user']['#item']['average'] +
$content['field_business_q2'][0]['user']['#item']['average'] +
$content['field_business_q3'][0]['user']['#item']['average'] +
$content['field_business_q4'][0]['user']['#item']['average'] +
$content['field_business_q5'][0]['user']['#item']['average'] +
$content['field_business_q6'][0]['user']['#item']['average'];
$total = $total / 6;
echo "<b>Total Average:</b> " . $total;
if ($total <= 20) {
echo '<img src="sites/all/files/images/shield1.png" align="right">';
} elseif ($total >= 21 && $total <= 40){
echo '<img src="sites/all/files/images/shield2.png" align="right">';
} elseif ($total >= 41 && $total <= 60){
echo '<img src="sites/all/files/images/shield3.png" align="right">';
} elseif ($total >= 61 && $total <= 89){
echo '<img src="sites/all/files/images/shield4.png" align="right">';
} elseif ($total >= 91){
echo '<img src="sites/all/files/images/shield5.png" align="right">';
}
Thats the PHP I use to get the average of averages... But that is only an aesthetic display. I need to use computed to get the average into the DB so I can use that field for a search criteria. I've tried with the sample code the computed field module gives you out of the box.. but I couldn't even get it to display a string.. or display at all for that matter.
EDIT: So I've been able to wrap my head around the computed field after lots of trial and error. I realize that what I need to do is query the database and retrieve the value of specific fields from the content type. Problem is, am not sure of the proper way to do a query using the computed fields. I found a question on here that had the following sample code which i've modified to try and get the values I need. Another Problem is, I need to retrieve values from the comment of the node. So I'm not sure "$nid = $node->nid;" Will work. I will try this a post results.
$nid = $node->nid;
$result = db_query('SELECT field_buiness_q1_rating FROM hts_field_data_field_business_q1 WHERE entity_id = :nid', array(':nid' => $nid));
$entity_field[0]['value'] = $result;
RESULTS:
Error message Notice: Undefined variable: node in eval() (line 1 of /sites/all/modules/computed_field/computed_field.module(394) : eval()'d code). Notice: Trying to get property of non-object in eval() (line 1 of /sites/all/modules/computed_field/computed_field.module(394) : eval()'d code). PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'field_buiness_q1_rating' in 'field list': SELECT field_buiness_q1_rating FROM hts_field_data_field_business_q1 WHERE entity_id = :nid; Array ( [:nid] => ) in eval() (line 2 of /sites/all/modules/computed_field/computed_field.module(394) : eval()'d code).
EDIT: Tried the following code;
$select = db_query('SELECT * FROM {hts_field_data_field_business_q1}');
$tally = "";
foreach ($select as $row){ $tally = $tally + $row['hts_field_data_field_business_q1']);}
$entity_field[0]['value'] = $tally;
RESULT: Didn't get a query error but it doesn't save anything into the db.