ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

How to prevent duplicate entries in a {categories} loop

August 25, 2011 8:36pm

Subscribe [1]
  • #1 / Aug 25, 2011 8:36pm

    Garrett

    36 posts

    Hi Everyone,

    I’m using {exp:super_search:results} to output channel entry info. First I’ll describe my goal, then show how I’m going about it.

    Within the results loop, I need to check what categories have been assigned to the entry, and see if any of those categories match the categories that the logged in user is assigned to (I’m using Solspace User to assign users to multiple categories). At this point in the code, I have a pipe-delimited list of what categories the member belongs to, for example “10|12”. I need to check if the current entry belongs to either of those categories, and if it does, display the {title}. This works fine with the code below, except when the entry and the user are both assigned to more than one of the same category, in which case I get duplicate entries displayed, because the condition evaluates to true for each matching category.  So I am trying to figure out how to prevent this. Here’s my code:

    {exp:super_search:results}
    {categories}
    {if category_id == '10' OR category_id == '12'} {!-- I use an embed variable to generate this condition dynamically --}
    {title} 
    
    {/if}
    {/categories}
    {/exp:super_search_results}

    So if I have an entry that has been assigned to category 10 and 12, the {title} is displayed twice.

    Normally I would just hard code the appropriate categories to search in the results loop, which would prevent duplicates, but I need to allow the user to search based on other categories that will appear in the URL. That’s why I have a separate process within the results loop to ensure that, ultimately, only entries in the right “member group” categories are displayed.

    Hopefully this makes sense. I can’t figure out an elegant way of preventing duplicates from being displayed. Perhaps I could write a plugin that answers this question:

    Does this entry belong to any of these category_ids? (10|12).
    If yes, do something, 
    if not, go to the next iteration of the results loop

    This would avoid the looping nature of the {categories} tag. But, I’m not sure I am up to that challenge yet. Any creative ideas anyone? Or any advice on how I might achieve this with a plugin?

  • #2 / Aug 26, 2011 1:46pm

    Garrett

    36 posts

    Here’s a solution I’ve found. I’m not sure how efficient it is, as now I’m doing a SQL query for every entry returned. But at least I got rid of the embed I was having to use. I put this query within my super_search:results loop, so it checks to make sure each returned result belongs to at least one of the categories that the user is assigned to.

    {exp:query sql="SELECT exp_category_posts.entry_id
                          FROM exp_category_posts, exp_user_category_posts
                          WHERE exp_category_posts.cat_id = exp_user_category_posts.cat_id 
                          AND exp_user_category_posts.member_id='<?php echo $this->EE->session->userdata('member_id');?>'
                          AND exp_category_posts.entry_id='{entry_id}'
                          LIMIT 1"}
    
                          {if {entry_id} != ''}
                            {entry_id} - {title}
    
                          {/if}
    {/exp:query}
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases