HomeDrupal 7 + views PHP using global PHP fields


Drupal 7 + views PHP using global PHP fields

Background:
Drupal 7 using views PHP and Global: PHP fields.

Problem:
For some reason, and I don’t know why, the available variables, shown in the field below value code and output code, in global php field return the NID, and not the actual value. I also do not understand the difference between the value code and the output code. They interpret the exact same code differently and I find it extremely annoying. So I default to using the output code only. This will be a headache if you have to reuse the value you output.

Why am I doing this? I need to run queries on the tables in the drupal database for financial analysis.

Solution:
So if the available variables are not what you need then how do you find what you need? Its simple, add this code to the output code field:

<?php
print_r($data);
?>

What this does is spits out all the available data values and you have to muddle through it to find the data you need. You can drill down farther if you need to:

<?php
print_r($data->field_field_whateverfield);
?>

In my case, this was the winning solution:

$data->field_field_project_number_s['0']['raw']['value'];

My final code in the output code field:

<?php

$pn = $data->field_field_project_number_s['0']['raw']['value'];

$an = $data->field_field_award_number_s['0']['raw']['value'];

$tcb = db_query("

SELECT SUM( field_cost_budget_value )

FROM field_data_field_cost_budget, field_data_field_award_number_s, field_data_field_project_number_s

WHERE field_award_number_s_value = '$an'

AND field_project_number_s_value = '$pn'

AND field_data_field_cost_budget.entity_id = field_data_field_award_number_s.entity_id

AND field_data_field_cost_budget.entity_id = field_data_field_project_number_s.entity_id")->fetchField();

print $tcb;

?>

Remember, this only prints the variable $tcb. You will not be able to access $tcb again. I haven’t found a solution for this either. Hopefully I can find answers to some of these issues, if so I will post.

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.