I'm setting up a client database. I have a node with a field showing the value of the client's house and another field showing the balance of the mortgage. When the node is viewed, I want it to calculate the difference.

This doesn't seem a very difficult thing. Can I do this with Views? Do I have to hack a module/theme to customise the way that the node is displayed?

I'm new to Drupal by the way. Thanks in advance!

Comments

matt_harrold’s picture

You could use Views and Views Customfield, you could create a little custom PHP block, you could put the calculation directly into the node.tpl.php file of your theme (not the best idea IMHO), or use Contemplate to format your node and include the calculation. There are many options.

Here's a little bit of PHP you could put into a Block, to be shown on node/* pages ...

if((arg(0)=='node')&(is_numeric(arg(1)))){
  $node=node_load(arg(1));
  if($node->type=='your_content_type'){
     $value = $node->field_house['value'] - $node->field_mortgage['value'];
     print $value;
  }
}
//print_r($node);

The names of your CCK fields will undoubtedly be different, as will your_content_type ... these will need to be fixed before it will work. I also assumed the "calculation" was a simple subtraction.

The advantage of using a block is it can be placed just about anywhere. The disadvantage is it isn't easy to put the results "inline" with your node.

rlp’s picture

Matt--I'm astonished at how quickly you've come back to me and how thorough your answer is. Thanks so much, and I'll post back when I try to implement your suggestion.

Thanks again.