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.

What's wrong with this query?

June 08, 2012 6:22pm

Subscribe [1]
  • #1 / Jun 08, 2012 6:22pm

    Matthew81

    81 posts

    In my ongoing quest to create a header snippet that will dynamically assign <title> and <meta> values based on the type of content being viewed, I wrote the following code:

    {if segment_1}
     {exp:query sql="SELECT exp_channels.channel_title, exp_channels.channel_description, exp_categories.cat_name, exp_categories.cat_description FROM exp_channels, exp_categories WHERE exp_channels.channel_name='{last_segment}' OR exp_categories.cat_url_title='{last_segment}'"}
      {if total_results=='1'}
       <title>{channel_title} - {cat_name}</title>
      {/if}
      {if total_results>'1'}
       <title>{total_results}</title>
      {/if}
      {if no_results}
       <title>No Results</title>
      {/if}
     {/exp:query}
    {/if}

    The goal is to select only one row from EITHER the exp_channels or exp_categories table (assuming unique channel/category names).

    The problem is that when I’m viewing a page where {last_segment} is a channel, then the query returns a row for each of my categories, and when I’m viewing a page where {last_segment} is a category, it returns a row for each of my channels.

    Obviously something is either wrong with my query, or with how EE is handling it. I don’t have a great understanding of selecting from multiple tables at once so I’m guessing I’m the one who’s wrong, but would appreciate any insight on what I can do to fix it.

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

ExpressionEngine News!

#eecms, #events, #releases