2

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.

4
  • If you can't get the sample code of the module to work, then there is very likely something wrong with your site/configuration. You should try to get the sample code working first before trying something more complex. If you can't get the sample code working, you should file a support request on the module's issue queue. Commented Apr 23, 2012 at 19:00
  • I was able to get a simple string into the field but I still wasn't able to display it. So I believe computed field is installed correctly. Commented Apr 25, 2012 at 22:36
  • I would like some help on this question as this would REALLY move my work project along... Commented May 8, 2012 at 16:13
  • To increase the chance that your question will be answered, update your question from time to time with what you have tried as you work toward the solution of the problem. This will bump your question to the top of the list. Adding a comment, by contrast, does nothing. Commented May 9, 2012 at 2:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.