Take the 2-minute tour ×
ExpressionEngine® Answers is a question and answer site for administrators, end users, developers and designers for ExpressionEngine® CMS. It's 100% free, no registration required.

I need to restructure the channel entries (I'm trying to group them by one of the channel fields). For example, say I have a list of fruits and each fruit had a color. I'm trying to group these fruits in an associative array by color, like:

$fruits['yellow'] = array('fruit_name' => 'banana' ...

Once I have this restructured data, I then need to loop over it and output certain mark up.

So far, what I have done is to set Allow PHP? to Yes and declared an array, built that array within the entries tag pair, and now going to loop through that array and output the markup I need.

This seems messy. Is there a "cleaner" approach?

UPDATE

This is how I want to output the data, which I think would be impossible to achieve with an orderby:

Yellow
 -- Banana
 -- Lemon
 -- Papaya

Red
 -- Apple
 -- Strawberry
 -- Cherry

So, I need to loop over the colors, then loop over the fruits within each color. It's a nested array that I need. Using orderby does not give me a nested array.

share|improve this question

5 Answers 5

up vote 3 down vote accepted

Perhaps you could use the orderby parameter on your custom field, and use PHP to monitor that field, and, each time it changes, perform your custom display behavior?

Edit 1: Setting up Categories

It turns out that you should probably be using categories to display your entries. Using your example above, you should setup a Category Group; there are many strategies for managing categories/sub categories; we'll assume you just need a single Category Group called "Colors."

In any event, create the "Colors" category group (Admin > Channel Administration > Categories > [Create a New Category Group] > Name it "Colors" > Add/Edit Categories > etc), then, tie it to your channel (Admin > Channel Administration > Channels > Edit Group Assignments > [Category Groups]).

After jumping through those hoops, you'll be able to specify which colors are associated with each entry when editing them.

Once categories are in established, displaying them in a template is straight forward:

{exp:channel:categories style="linear"}
  <h3>{category_name}</h3>
  {exp:channel:entries category="{category_id}"}
    <p><a href="{page_url}">{title}</a></p>
  {/exp:channel:entries}
{/exp:channel:categories}
share|improve this answer
1  
I really think I need a nested array. Question updated. –  ExpressionEngineNewbie Dec 3 '12 at 20:41
1  
I guess the followup question would then be asking why you're using a custom field and not a category? –  Nick Benson Dec 3 '12 at 20:45
    
I'm new to EE. Should "yellow" have been a category instead? If so, how do I do that? I just used a simple Select Dropdown to contain the thing I wanted to group by. –  ExpressionEngineNewbie Dec 3 '12 at 20:48
    
Yes. I'll put together an answer for you, standby. –  Nick Benson Dec 3 '12 at 20:58
    
Thanks. I'm looking at ellislab.com/expressionengine/user-guide/modules/channel/…, but I'm not sure if this actually shows me how to set things up. It would be great if I could somehow convert my Select Dropdown to this category feature. –  ExpressionEngineNewbie Dec 3 '12 at 21:04

As others have noted comments could be the best way to go for you. But if you have valid reasons to use a custom field for fruit colors you can group entries by color with the third-party NSM Channels add-on (AUD $39.95)

This should work:

{exp:nsm_channels:group_entries_by
  group_by="custom_field_color"
  }
  <h3>{custom_field_color}</h3>
  <ul>
  {channel_entries channel="fruit"}
    <li>{title}</li>
  {/channel_entries}
  </ul>
{/exp:nsm_channels:group_entries_by}
share|improve this answer

Is there a specific reason you can't use the entries order by parameter to order by that field? I think understanding that would help us answer this question.

share|improve this answer

You could use Stash to filter by colour on output with match/against:

{exp:stash:set_list name="fruit" parse_tags="yes"}
    {exp:channel:entries channel="fruit"}   
        {stash:title}{title}{/stash:title}
        {stash:color}{cf_color}{/stash:color}
    {/exp:channel:entries}
{/exp:stash:set_list}

{exp:stash:get_list name="fruit" match="#^yellow$#" against="color"}
{title}
{/exp:stash:get_list}

https://github.com/croxton/Stash/tree/dev

share|improve this answer
    
Note that a category group for 'Colors' would be a more appropriate grouping solution, unless you have a particular reason to use a custom field. –  Mark Croxton Dec 3 '12 at 21:14
    
No particular reason for using Select Dropdown. I'm still navigating my way around EE. Will look into the categories solution first. –  ExpressionEngineNewbie Dec 3 '12 at 21:20

Have a look at http://devot-ee.com/add-ons/wb-category-select field type It creates a drop-down of one or more category group.

Also one of the best category plugins around - http://devot-ee.com/add-ons/gwcode-categories you can use it to generate nested or simple list of categories or custom category fields.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.