Create age groups of visitors
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:
$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:
$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.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion