How to calculate values of another node with computed field?

GiRo - August 23, 2008 - 07:47

With computed filed one can make calculations of two or more fields of a node like explained here: http://drupal.org/node/149233
That's clear and I used that already successfully. But how can I use for example values that are in node/1 in a computed field that is used in node/2?

Well. You'll need to do the

p6 - August 23, 2008 - 08:42

Well. You'll need to do the following

  • Set up a CCK Noderef field first, to refer to node(s) that you want
  • Use computed field to grab the nid, load the node and make available that node's fields for computation

You can see something similar here (sort of): http://drupal.org/node/87546

- Pavan Keshavamurthy
http://grahana.net/

Thanks a lot. Got it finally

GiRo - August 24, 2008 - 23:01

Thanks a lot. Got it finally working with this code:

$node_field[0]['value'] = db_result(db_query("SELECT field_YOURFIELDNAME_value FROM content_type_YOURCONTENTTYPE WHERE nid=%d",$node->field_YOURNODEREFERENCEFIELDNAME[0][nid])) * $node->field_THISNODESFIELD[0]['value'] ;

Notes:
YOURFIELDNAME is the field name of the other node's field to which the node reference field is referring to. You need to add '_value' to the field name itself as the field itself is stored with that appendix in the MySQL-Table.
YOURCONTENTTYPE is the content type of the node referred to.
YOURNODEREFERENCEFIELDNAME is the field name of the node reference field.
THISNODESFIELD is the field of the node where this computed field is used.

Cheers!

what's the THISNODEFIELD for?

asak - October 17, 2008 - 22:18

Quick question:

THISNODESFIELD is the field of the node where this computed field is used.

You mean the name of the computed field this code is in?

I've used that exact code without the last quoted part succesfully, so i wonder what will adding * $node->field_THISNODESFIELD[0]['value'] actually do...?

BTW - would you know how to get the LABEL of the field instead of the KEY (I'm using allowed values like y|Yes n|No etc.)

with

GiRo - November 13, 2008 - 21:41

with $node->field_THISNODESFIELD[0]['value'] I mean any field in the node where the computed field is located.

My question was:
But how can I use for example values that are in node/1 in a computed field that is used in node/2?

So with db_result(db_query("SELECT field_YOURFIELDNAME_value FROM content_type_YOURCONTENTTYPE WHERE nid=%d",$node->field_YOURNODEREFERENCEFIELDNAME[0][nid])) I get a db query done to a field in node/1.

The computed field is in node/2
And with $node->field_THISNODESFIELD[0]['value'] I address simply a field in node/2.
With * I just multiply this two values, you can also do any other operation.

This code do well if the

Alpha5 - December 7, 2008 - 09:40

This code do well if the result is not an array. If the YOURCONTENTTYPE have revision, then will have multi result. Can someone have code to work even if content type reference have revision or not?

 
 

Drupal is a registered trademark of Dries Buytaert.