I cannot get proper results from View PHP field.

I've set up a Flipped Table view of entity form submission of which I'm trying to calculate "results" (sum and count(for avg)) with following code

$results = array(); // For results
$static = get_object_vars($row);
dsm($static);
foreach($static as $key => $value) {
    if(strpos($key,'field_') !== FALSE){
        if($value !== NULL){
            $results[$static['entityform_id']]['count']++; // Count no. of fields
            $results[$static['entityform_id']]['sum']= $results[$static['entityform_id']]['sum']+$value; // Count sum of field values
        }
    }
}
return json_encode($results[$static['entityform_id']]);

which results:

{"count":55,"sum":55}   {"count":55,"sum":110}  {"count":55,"sum":165}  {"count":55,"sum":220}

At this point I do not understand that why sum is always row index * sum? Eg.1 =55, 2=110, 3=165... All the submissions have the same fields and field values (clones) which explaines sums but I can't figure out that where this multiplication "logic" happens?

It seems like it is looping/counting all four result rows every time.

This is because $results[$static['entityform_id']]['sum'] already contains the sum result from previous loop result. Thus it adds 55 for every row iteration. Try adding the current array index to $results inside foreach loop.

  • How can it contain since entityform_id is different(1...4) and being used as an array index to separate calculated values for each submission? It should create new index and therefore value should be empty. But I'll try to use some other method for indexes and we'll see what then happens... – user1271930 Sep 2 '15 at 16:39
  • Correct me if i am wrong.. How can entity form id be different if you are iterating over the submissions of a particular form.. – Swarnendu Dutta Sep 2 '15 at 17:17
  • Yeah, I know. That's a bit misleading. In Views guidelines it is named like that as "entityform id" but the description states that it is actually submission ID. And I did verify it by debugging. – user1271930 Sep 2 '15 at 19:00
  • Okay, now that I tried to debug the actual field values it shows that all values are overriden. For the first submission value is 1 and so on until 4 (submission id). And to get these results I'm only using print function to view $row variable and not modifying any variable. Really weird... – user1271930 Sep 3 '15 at 9:25
  • Did you try creating views handlers for your entity fields ?? That way you can get more flexibility with operations and customizations on the fields. – Swarnendu Dutta Sep 3 '15 at 17:57

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.