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.

Excluding categories from a certain group

February 16, 2012 12:37pm

Subscribe [2]
  • #1 / Feb 16, 2012 12:37pm

    studio-d

    7 posts

    I am working on an EE 1.6.8 site and need to conditionally display search results based on categories. However, I am getting duplicated content in my results.

    {exp:search:search_results disable="member_data|trackbacks" orderby="date" paginate="bottom" limit="30" show_expired="no" show_future_entries="no"}
    
          {exp:localisation:local_query  local_sql_url_title="content-from-url-title" page_url_title="{url_title}" channel_ids="6,9"} 
          
           
           {exp:query sql="{local_sql_query}" show_future_entries="no" }
               {categories}
         {if category_name == "Videos"} {category_name}
        content
         content
           
        {if:elseif category_name == "Images"} {category_name}
         content
          
         {if:elseif weblog_short_name == "press-releases" AND category_name != "Videos" AND category_name != "Audio" AND category_name != "Images"} {category_name}
          content
          
          
          
         {if:elseif weblog_short_name == "articles" AND category_name != "Videos" AND category_name != "Audio"  AND category_name != "Images"} {category_name}
         
           content
          
    
        
         {if:elseif weblog_short_name == "press-kits" AND category_name != "Videos" AND category_name != "Audio" AND category_name != "Images"} {category_name}
         content
          
    
    
         
         {/if}
        
        
        {/categories}
               {/exp:query}
           
           
          {/exp:localisation:local_query } 
        {/exp:search:search_results}

    The problem is that an item of content might have a category assigned called “Videos” and another category assigned called “Global” which I think might be in a different category group called “Regions”, if such a thing exists in EE. So I am getting duplicated results for some articles - one for each of the categories it is assigned to.

    Does anyone have a way round this?

  • #2 / Feb 17, 2012 2:53pm

    Dan Decker

    7338 posts

    Hi studio-d,

    You are correct, ExpressionEngine will return a result for each category that an entry is assigned to.

    It appears you are using a third-party add-on to build your query, is there anyway you can show the full query?

    ...a different category group called “Regions”, if such a thing exists in EE.

    Category and Category group names are completely up to the developer. It is very likely that this install has a group named Regions and Global.

    Cheers,

  • #3 / Feb 20, 2012 5:43am

    studio-d

    7 posts

    Hi,

    Thanks for your reply. It is using a custom localisation module which runs queries as follows:

    function local_query(){
      
       global $TMPL;
       global $DB; 
       $url_title = $TMPL->fetch_param('local_sql_url_title');
       $page_url_title = $TMPL->fetch_param('page_url_title');
       $category_url_title = $TMPL->fetch_param('category_url_title');
       $channel_ids = $TMPL->fetch_param('channel_ids');
       $result_limit = $TMPL->fetch_param('result_limit');
       
       /*when we build the SQL string, we may or may not need to set a result limit.  
       The format for the result limit will be "startFromInt,returnRowsInt" (e.g. 0,5 to start at teh beginning and return five rows.
       We still need to add the keyword LIMIT to make this work in SQL
       */
       if(isset($result_limit)){
       if(strlen($result_limit) >= 1){
        $result_limit = " LIMIT ".$result_limit ;
        } 
       }
       
       
       // get sql from channel/weblog
      
       //get weblog_id and entry_id from title table using the local sql title param passed in     
       $title_table_query = "SELECT entry_id, weblog_id from exp_weblog_titles where url_title ='".$DB->escape_str($url_title)."'";
       $title_table_results = $DB->query($title_table_query);
        
       //get the sql from the weblog_data table using using the weblog_id and entry_id from the previous query
        $data_table_query = "SELECT field_id_61 FROM exp_weblog_data WHERE weblog_id =".$title_table_results->row['weblog_id']."  AND entry_id =". $title_table_results->row['entry_id'];
        $data_table_results =  $DB->query($data_table_query);
        
       //Find and replace {cookie} with $_COOKIE['exp_cklocale'] value which is the users locale
       $local_query =  str_replace('{cookie}',$_COOKIE['exp_cklocale'],$data_table_results->row['field_id_61'] );
       
       $local_query =  str_replace('{url_trtitle}',$page_url_title,$local_query );
       
       $local_query =  str_replace('{category_url_title}',$category_url_title,$local_query );
       
       $local_query =  str_replace('{channel_ids}',$channel_ids,$local_query );
       
        $local_query =  str_replace('{result_limit}',$result_limit,$local_query );
       
       
      
       //execute query and return piped ids
      // $local_entry_ids_result = $DB->query($local_query);
         
      $this->locale_sql_query = $local_query;
       
       $tagdata = $TMPL->tagdata;
       
      $tagdata = str_replace('{local_sql_query}', $this->locale_sql_query, $tagdata);
      
       return $this->return_data = $tagdata;
      
     }

    There is some strange voodoo going on which seems to let a template embed queries within each other. The search results page appears to be running approx. 100 queries if I switch on ‘Display SQL Queries? ’ in the control panel.

    What I’m after is a way to display the results without the duplicates.

  • #4 / Feb 24, 2012 12:32pm

    Robin Sowell

    13255 posts

    Strange voodoo indeed!  What it looks like is going on- you are nesting that {exp:localisation:local_query} tag inside the search results- so it’s going to run once for every result you have- passing the addon the url_title parameter.  Why does it pass the url_title instead of the entry_id?  Because you then do a query to get the entry_id based on the url_title.  As far as I can see- you could have passed it the entry_id and then saved that query- which runs 30 times if you get a full page of results?

    That aside- it then gets the custom field- field_id_61- for that entry_id.  Which can add another 30 queries.  And then it does some string replacing to generate $local_query- which it returns, and which is then used as the sql in the query module- for up to another 30 queries.

    I would personally not structure things this way- the nested/looping queries doesn’t scale all that well.  But I’m not entirely sure that’s the problem.  Can you lay out what the whole localization bit is doing?  I might be able to help brainstorm a simpler solution.

    Also- I’m fuzzy on where it’s getting $local_query - what’s in that custom field?  From looking at the code, it’s a query?  If you look at the template debugging- look for the query module, it should show you the parameters, including the sql parameter.  What’s the query look like for one loop of that?

    Sorry for all the questions- just trying to detangle what part of this is the search module and what part is the custom code.  From here, I’m just not sure.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases