Template variable issue

Events happening in the community are now at Drupal community events on www.drupal.org.
rcharamella's picture

Hi all,

I'm having a issue with a variable I've created as a template variable. I imagine I'm missing something really basic.

You can see the result at rnp.org/beta, move from page to page and see the value inside "Currently Power... change.

$MW = db_query("SELECT SUM(field_project_capacity_value) FROM drpl_content_type_project WHERE field_project_capacity_unit_value = 'MW'");
$int_MW = (int)$MW;
$KW = db_query("SELECT SUM(field_project_capacity_value) FROM drpl_content_type_project WHERE field_project_capacity_unit_value = 'KW'");
$int_KW = (int)$KW;
$KW_to_MW = ($int_KW / 1000);
$final_MW = ($int_MW + $KW_to_MW);
$households = ((($final_MW * .33) * 8760) / 11965) * 1000;
$int_households = number_format($households);

What do I need to do so that the variable doesn't change as we move from page to page? Is phptemplate_preprocess_page the wrong preprocess variable to use? Or should this be done a different way?

Here's the scenario:
I created a phptemplate_preprocess_page variable.
I'm querying the database for some information from a custom content type. That part works.
But when I call the variable in my pages, the result changes as you navigate from page to page.

Your assistance is greatly appreciated.

Comments

cache

Elliot Schlegelmilch's picture

This seems like something to cache: just set the timeout to how often you'd like it to update.

$val = cache_get('households');
if ($val == 0) {

// your calculations
cache_set();
} else {
$int_households = $val;
}

Why not just store it in the

tom.camp's picture

Why not just store it in the variable table?

variable_set( 'num_households', number_format( $households ) );

Then you can retrieve it from where ever you like.

I realize that you are then querying the db again, but who cares, that query will take microseconds and will have a negligible effect on performance.

When in doubt, go downhill.

I suspect you have page

mikey_p's picture

I suspect you have page caching on, and the result it being cached as the value present when the cache is generated for each page. Try turning caching off, or clearing the cache fully whenever a new node is saved.

Portland (Oregon)

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: