Hi everyone
I am trying to generate a simple list of categories. Each category name should be output once.
This code outputs lots of duplicates which is not what I want:
{exp:file:entries directory_id="8"}
{categories}
<li><a href="http://{path=%27downloads/index%27}" class="current">{category_name}</a></li>
{/categories}
{/exp:file:entries}
How can this be done?
Thanks for your help.
Hm. There’s not a way to do this with the current code you have. We may need to find another way to approach the issue. Right now your code is looping through each file and outputting the categories associated with the file. This is why categories will duplicate, the code is specifically telling it to list out categories for each file.
I’m assuming what you’re trying to do is only list categories that are being used by files in that directory. For example, if that directory has 16 categories assigned to it via the category groups, but the files in that directory are only using 8 of those categories, you only want those 8 displayed, correct? Almost sounds like what you need is an {exp:file:categories}
tag.
If this is correct, then I think you’re only option right now is do this via the query module and query the exp_file_categories table.
Hi Andy
I am simply trying to list the Categories in the Category group that is associated with my “downloads” upload folder. Nothing more.
I am surprised that this is so difficult to achieve with EE.
Can you give me a hint as to what the query with the query module would have to look like?
Thanks a lot for your help.
You’ll always run into things that EE doesn’t do out of the box that you wish it did. Just like any framework. I think what’s cool about EE is tools like the Query Module that lets you quickly do something custom that’s not native and how easy it is to quickly build an add-on or such to do things like this too if you want to push it to do more.
With Query Mooudlue installed, here’s what you’re looking for:
{exp:query sql="SELECT DISTINCT cat.cat_name FROM exp_categories cat JOIN exp_file_categories fcat on cat.cat_id = fcat.cat_id "}
<li><a href="http://{path=%27downloads/index%27}" class="current">{cat_name}</a></li>
{/exp:query}
Hope that helps. Let me know if you have any more issues.
> Is it safe to use the query module in terms of SQL injections directly in the template code?
Yes. Any SQL statements you write in the are passed through the EE Database Class using $query = ee()->db->query($sql);
> Is there really no simpler way to return a simple list of categories from a category group X?
That is a very simple task. However, according to your opening post, you are trying to only return categories where files have chosen those categories. That’s a little more complicated. If you’re trying to simply list out categories just use the {exp:channel:categories}
tag. Docs: https://docs.expressionengine.com/latest/channels/categories.html
> Thanks for your code snippet. But unfortunately, I can’t get it to work here. It returns: nothing. No error. Just nothing.
For that query to work, you need to have files that have categories associated with them. Either go through some files and associate categories or manually look at your exp_file_categories
files tables to ensure there is data there.
Hi Andy
I think the problem is that I want to output categories that are not part of a channel. My categories are simply associated with files in the FileManager, not with any channel. And I simply want to output all categories of the category group. No restrictions or filtering necessary.
To be honest I am a bit disappointed that EE can’t do this.
Something like this would suffice:
{exp:categories category_group="3"}
{category_name}
{/exp:categories}
But it seems that categories can’t exist outside of channels in EE? Strange…
I’ll admit categories in EE could do with an overhaul. Having categories apply only to files is not a typical request which is why all the listing capabilities are around channels.
I think I can create a quick add-on for you to use for this. Basically to display all categories associated with File Upload Locations or you can specify a directory_id and only display categories for that location. Let me know if you need it to do anything else. I’ll try to whip it up today.
Hi Andy
Thanks so much. This is what I love about EE: the community and support (even for non-paying users!).
But don’t worry, I just converted all my photos and downloads to channels so I can create a proper categories menu for them.
I can link File Upload folders to Category groups in the EE backend, so I was under the assumption that I can also filter those Files in the frontend easily with EE. However, this is not the case. Lesson learnt. I will probably stick to Channels in the future. Makes everything a lot easier…
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.