We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

[SOLVED] How to output category names without duplicates?

How Do I?

Tintin81's avatar
Tintin81
116 posts
4 years ago
Tintin81's avatar Tintin81

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.

       
Andy McCormick's avatar
Andy McCormick
183 posts
4 years ago
Andy McCormick's avatar Andy McCormick

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.

       
Tintin81's avatar
Tintin81
116 posts
4 years ago
Tintin81's avatar Tintin81

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.

       
Andy McCormick's avatar
Andy McCormick
183 posts
4 years ago
Andy McCormick's avatar Andy McCormick

let me try to do it myself and I’ll send you the snippet.

       
Andy McCormick's avatar
Andy McCormick
183 posts
4 years ago
Andy McCormick's avatar Andy McCormick

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.

       
vw000's avatar
vw000
241 posts
4 years ago
vw000's avatar vw000

Is it safe to use the query module in terms of SQL injections directly in the template code?

Or is there anything special that has to be taken into account while building queries? I ask this because usually with SQL you would use prepared statements first.

       
Tintin81's avatar
Tintin81
116 posts
4 years ago
Tintin81's avatar Tintin81

Hi Andy

Thanks for your code snippet. But unfortunately, I can’t get it to work here. It returns: nothing. No error. Just nothing.

Is there really no simpler way to return a simple list of categories from a category group X?

       
vw000's avatar
vw000
241 posts
4 years ago
vw000's avatar vw000

Do you have the query modules enabled?

       
Tintin81's avatar
Tintin81
116 posts
4 years ago
Tintin81's avatar Tintin81

Yes, sure!

       
Andy McCormick's avatar
Andy McCormick
183 posts
4 years ago
Andy McCormick's avatar Andy McCormick

> 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.

       
Tintin81's avatar
Tintin81
116 posts
4 years ago
Tintin81's avatar Tintin81

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…

       
Andy McCormick's avatar
Andy McCormick
183 posts
4 years ago
Andy McCormick's avatar Andy McCormick

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.

       
Tintin81's avatar
Tintin81
116 posts
4 years ago
Tintin81's avatar Tintin81

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…

       
Andy McCormick's avatar
Andy McCormick
183 posts
4 years ago
Andy McCormick's avatar Andy McCormick

ok. sounds good. Let me know if you have any trouble with that approach. I’ll be happy to help.

       
OliviaParcker's avatar
OliviaParcker
2 posts
4 years ago
OliviaParcker's avatar OliviaParcker

I started filtering files in the frontend using EE and it worked for me too. thanks for your advice

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.