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.

no_results not working in exp:query

October 31, 2011 11:16am

Subscribe [2]
  • #1 / Oct 31, 2011 11:16am

    tcast

    4 posts

    {exp:query sql="SELECT v.entry_id, v.url_title, v.title, v.entry_date, hs.photo, hs.filelength, hs.status, hs.private FROM exp_channel_titles v, exp_horseshow_videos hs
    WHERE v.author_id = {segment_3}
    AND v.channel_id = 5
    AND hs.entry_id = v.entry_id
    LIMIT 0,4"}
      {if no_results}No videos found{/if}
      {if private == "no" OR segment_3 == logged_in_member_id}
      <li>
      {if "{photo}"}
       <a href="http://{path=view}/{url_title}" title="{title}">{photo}</a>
      {if:else}
       <a href="http://{path=view}/{url_title}" title="{title}">{site_url}images/video_thumbs/not-set.png</a>
      {/if}
      <div>
       <strong><a href="http://{path=view}/{url_title}">{title}</a></strong>{if logged_in_member_id == "{segment_3}"} <a href="#">(edit)</a>{/if}
    
       <span>{if filelength}{filelength format="%i:%s"}{/if}</span>
       {if private == "yes"}
    <span>PRIVATE</span>{/if}
      </div>
      </li>
      {/if}
     {/exp:query}

    When I run the query in mysql, it works fine. Either returns a result or returns num_rows = 0. But no matter whether it returns rows or not, the {if no_results} tag doesn’t work. Any ideas?

  • #2 / Nov 01, 2011 2:25am

    John Henry Donovan

    12339 posts

    Hi tcast,

    What version and build of EE are you using?
    Is this query tag nested inside any other tag?

    Be sure and wrap some single quotes around you segment variable so it is sanitized

    can you strip everything back to this please and put inside a blank template to test

    {exp:query sql="SELECT v.entry_id, v.url_title, v.title, v.entry_date, hs.photo, hs.filelength, hs.status, hs.private FROM exp_channel_titles v, exp_horseshow_videos hs
    WHERE v.author_id = '{segment_3}'
    AND v.channel_id = 5
    AND hs.entry_id = v.entry_id
    LIMIT 4"}
      {if no_results}No videos found{/if}
      {title}
    
     {/exp:query}

    But no matter whether it returns rows or not, the {if no_results} tag doesn’t work.

    If no results will ignore all other markup in the query tag and only display whats inside those tags if no rows are returned

  • #3 / Nov 01, 2011 12:25pm

    tcast

    4 posts

    Hey John,

    doing some testing now based on what you said. The query tag is nested inside several others, like {exp:friends:member} (via Solspace), and a custom conditional I created to test for privacy settings. One of those is breaking the query no_results. When I move the query outside of all the other template data, it works.

    I’m assuming that no_results is getting set by one of the other tags and not allowed to be overwritten inside the local scope of the {exp:query} tag. Is there a way I can do a local results count? i.e. {if query_count == 0} or something.

  • #4 / Nov 03, 2011 12:40pm

    Robin Sowell

    13255 posts

    tcast- you have the gist of it.  Especially with that conditional?  Nesting can result in hijacking. 

    I like your conditional option- try total_results- if that will do the trick, seems the easiest way to go.  Could use an embed- but don’t love this one for performance and ‘it’s kinda messy’ reasons. 

    Let us know if the conditional does it- if not, can brainstorm some options.

  • #5 / Nov 03, 2011 3:38pm

    tcast

    4 posts

    Nope… Doesn’t work. I can’t use that tag inside a query that returns zero rows, because it won’t get looped through at all.

  • #6 / Nov 03, 2011 4:01pm

    Robin Sowell

    13255 posts

    Bleh- doy.  Well, I’m not loving the options leaping to mind- you can move the query to its own template and embed- it will be parsed independently and should work.  Could use some php parsed on output:

    <?php
    $q_results = FALSE;
    ?>
    
    {exp:query sql="SELECT title FROM exp_channel_titles WHERE entry_id = '1' "}
    <?php $q_results = TRUE; ?>
    title: {title}
    {/exp:query}
    
    <?php
    if ($q_results)
    {
    echo 'has results';
    }
    else
    {
    echo 'no results';
    }
    ?>

    You get the gist- due to parse order, this shakes out ok.

    Trying to think of a quick plugin that could get around this and coming up a bit blank.

  • #7 / Nov 03, 2011 4:08pm

    tcast

    4 posts

    Seems like I’m always falling back to the same thing… pull it out into a custom module and do all the templating in a view. I like a lot of things about EE, but man, the templating system is such a huge handicap to everything I do.

    Appreciate the help.

  • #8 / Nov 03, 2011 4:38pm

    Robin Sowell

    13255 posts

    On the plus side- you can work around it and in a variety of ways.  But agreed- this would be a clunky one.  I’d be tempted to go custom route as well.

    Sorry I didn’t have an easier fix for you, tcast.

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

ExpressionEngine News!

#eecms, #events, #releases