Community Documentation

Counting the number of values in a multi value field

Last updated March 17, 2011. Created by whimsy on August 11, 2010.
Edited by Pomliane. Log in to edit this page.

I have a content type containing a multi-value image field (field_images). The number of images that can be added is unlimited. I'm using the Computed Field module which allows me to use php code to calculate the number of images that have been uploaded and display that total in a view.

When using the Computed Field module, put this code in the Computed Code field:

if (is_null($node->field_images[0]['fid']))
{
$node_field[0]['value'] = 0;
}
else
{
$node_field[0]['value'] = count($node->field_images);
}

Put this in the Display Format field:
$display = $node_field_item['value'];

Initially, I only used the count function without the preceding if statement. This did count correctly for nodes having at least one image; however, it would return a value of 1 for any node with no images. By looking at the database table, I figured out that when I add a node that has the field_images field, a row is created in the content_field_images table even if the node has no images. If there are no images, the field_images_fid field will be NULL. If you only count $node->field_images, the code will always return a minimum value of 1 because of that row in the table, even though it has a null value. So I had to first test for nodes with NULL in the field_images_fid field and return 0 for them. Hope this helps somebody else. I'm not a coder, so it took me two days to figure this out.

Comments

PROBLEMS WITH YOUR CODE

I HAD PROBLEMS WITH YOUR CODE.

ANYWAY I SUCCEDED IN OBTAINING THE RESULT WITH THIS CODE:

-------------------------------------------------------------------------
$node_field[0]['value'] = count($node->field_cliproadr[0]['nid']);
-------------------------------------------------------------------------

Did not work for me (last field empty, non null)

This code did not work for me. I have two problems:

  • Only the first item of the field is checked for null, not all items. If you add a new image, but don't upload anything to it, you want that item to be excluded as well. Note that if you edit a node, an empty item is automatically added. Also note that in the current version of imagefield, it is not possible to get rid of items you added, but not uploaded anything to. They do disappear on the next edit though.
  • For me, the items without any image uploaded to them don't have a NULL value, but 0 instead.

To get around these problems, I changed the code to:

$count1 = count($node->field_images);
$count2 = 0;

for($i = 0; $i < $count1; $i++) {
  $fid = $node->field_images[$i]['fid'];
  $n = is_null($fid) || $fid == 0;
  if (!$n) {
    $count2++;
  }
}

$node_field[0]['value'] = $count2;

This worked for me. Maybe other people will find this interesting as well.

Helped!

CCK Computed Field + this code $node_field[0]['value'] = count($node->field_images); solved my case.
If i was a PHP coder I think I could solve it with views — but I'm not.

Thanks s lot.

Does this work for D7?

Which code works also for D7?

This worked for me in D7

Computed code:
$entity_field[0]['value'] = count(field_get_items($entity_type, $entity, 'field_images'));
Display Code:
$display_output = $entity_field_item['value'];

Don't use count()

Overhauling an old site reminds me of a warning, for any who might still care...

Calling count() on the field simply counts all of the items in the array - including any empties. You must check for a positive fid; here is a very clean and paste-friendly method of computing this in Drupal 6:

$node_field[0]['value'] = 0;
foreach ($node->field_images as $v) if ($v['fid']) $node_field[0]['value']++;

Helped

I ammended the field names in original to reflect mine and worked like a charm. Thanks for life saving.

About this page

Drupal version
Drupal 6.x
Audience
Site users

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.