Last updated September 27, 2010. Created by amal850720 on September 27, 2010.
Log in to edit this page.

I maintain a community website and want to find out the age groups of my visitors. This age group is used for various purposes that require targeted audience especially in marketing where I need to send emails that of interest to the visitors in each age groups.

To reach there, I require visitors to register an account and filling their age born in a CCK field of type integer. Next, I added another CCK field of type computed field and exclude it from display. This computed CCK field is stored in the database as a sortable five-characters text field. The computed code follows:

<?php
   $currentyear
= date("Y");
  
$ageborn = $node->field_age_born[0]['value'];
  
$currentage = $currentyear - $ageborn;
   switch (
$currentage) {
      case (
$currentage >= 21 && $currentage <= 30):
           
$node_field[0]['value'] = "21-30";
      break;
      case (
$currentage >= 31 && $currentage <= 40):
           
$node_field[0]['value'] = "31-40";
      break;
      case (
$currentage >= 41 && $currentage <= 50):
           
$node_field[0]['value'] = "41-50";
      break;
      case (
$currentage >= 51 && $currentage <= 60):
           
$node_field[0]['value'] = "51-60";
      break;
      case (
$currentage > 60):
           
$node_field[0]['value'] = ">60";
      break;
   }
?>

The display format is left to its default as follows:

<?php
   $display
= $node_field_item['value'];
?>

Drop the PHP script opening and closing tags in the actual computed code text area.

This field is accessible in Views and can be used as a filter. Simply type the age group text (31-40, 41-50, ...) and Views will display the visitors that belong to the age group.

A caveat exists though, as I haven't found a way to make the age groups available as a dropdown select list. A quick workaround to this is to use Views Display Tabs, create five page displays that filter the five age groups respectively, and have them displayed as tabs. It appears to be more intuitive and informative.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

Is there another way to accomplish this. It is really important for these fields to exist in drop down or check boxes as their are various variables that can be set.

Head over to this link (http://drupal.org/node/908544#comment-4432076) for a reply on my question. I haven't tested it myself because I dealt directly with the database to do some tweaking. Perhaps the suggestion would help you.