tags:

views:

44

answers:

2

Explaine me please in simple words what is CCK Computed Field?

+5  A: 

It is a CCK Field that provides a "calculation" result you can add to any node. You can write custom php code to take some values from the node, or anywhere else in the database, and produce a result. Lets say you have a field on a node where someone enters their birthday. You can have a CCK Computed Field that uses php to calculate the persons age automatically without having to ask them to enter another peice of information:

<?php
$birthday_date = date_make_date($node->field__[0]['value']);
$birthday = $birthday_date->db->parts;

//compute age
$bdayunix = mktime(0, 0, 0, $birthday['mon'], $birthday['mday'], $birthday['year']);
$nowunix = time();
$unixage = $nowunix - $bdayunix;
$age = floor($unixage/ (365 * 24 * 60 * 60));

$node_field[0]['value'] = $age;
?>

Code Credit: Timur Gilfanov

A detailed handbook page on drupal.org can be found at http://drupal.org/node/126522

AdamG
its cleared. thanks.
Molfar
A: 

A calculated CCK field is one that is filled in when the record is saved. It is an actual field in the database, rather than one that is calculated only when it is to be shown.

I've used it to create a FullName field, when a user inputs his/her First and Last names.

The contents of the computed field can be anything you like, as AdamG notes above (although the age example isn't a good one because unless the record is updated, the age will never change). You put in your own PHP code that will be executed when the record is saved.

Often you don't need a computed CCK field; the calculations/changes can be done in a module or template.

Graham

related questions