I've been trying to figure out how to do something with Views in D7 for a couple of days and am stuck, and I wonder if someone could give me some suggestions.

I want to concatenate all the values from one column in all rows of my results where the other column(s) is/are equal --- so that the result is a single row (for each distinct value in the grouped column(s)) containing a list of the original values, separated by some separator such as a comma.

This is easier to explain with a simple example. Suppose I have a query that returns the following results:

NAME FRUIT
Fred apple
Fred pear
Fred bananna
Wilma apple
Wilma peach

What I want is the following:

NAME FRUIT
Fred apple,pear,bananna
Wilma apple,peach

This seems like a kind of "group by" or aggregation operation, but I can't seem to figure out a way to accomplish it with a view.

Is there a way to do this kind of thing with a view?

Another option that I've been looking into, if it's too difficult to get my view to return a result set like the second table above, is to have the view generate the (unaggregated) results like in the first table above, and then use a preprocess hook function in my theme's template.php to transform the results before they get rendered for display; then I could do the aggregation/concatenation in php. I did this kind of thing in D6 once and it worked, but I'm having trouble getting it to work in D7.

So, if it turns out that this kind of aggregation can't be done in a view and needs to be handled with custom php code, could someone give me some pointers on how to set up a function in D7 that I can use to transform the results of a view before they're rendered?

Thanks!
--Mark

Comments

rlnorthcutt’s picture

Looking for a similar grouping... can aggregation do this?

dawehner’s picture

No aggregation can't do this. You should probably use a custom field handler to load all fruits of a certain name. See advanced help for more information.

julia_g’s picture

Have you ever figured this out? I would be very grateful if you post your code. I need the same thing but don't have time to delve into Views API.

SilviuChingaru’s picture

+1 Subscribe

dawehner’s picture

Status: Active » Closed (duplicate)