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 output showing when there are results

July 23, 2012 1:28pm

Subscribe [3]
  • #1 / Jul 23, 2012 1:28pm

    Andrew Armitage

    86 posts

    We’ve a template which uses an embedded template to output the final data to screen based on results passed to it from the query module.

    Basically, we’re wanting to output events showing over the next month, but there may be 2 of the same events on the same day, so we only want to show this event once.

    The parent template is outputting the correct data from the database (with php enabled to convert times)

    {exp:query sql="SELECT exp_channel_titles.entry_id, exp_channel_titles.status, exp_channel_titles.entry_date, exp_channel_titles.expiration_date, exp_channel_data.field_id_33 AS eventID FROM exp_channel_titles INNER JOIN exp_channel_data ON exp_channel_data.entry_id = exp_channel_titles.entry_id WHERE exp_channel_data.channel_id = '26' AND exp_channel_titles.status = 'open' AND exp_channel_titles.entry_date < '<?=$monthMaxDate;?>' AND exp_channel_titles.entry_date > '<?=$monthMinDate;?>' GROUP BY eventID"}
    {embed="theatre/time-based-listing" embedded_event_id="{eventID}"}
    {/exp:query}

    Then in the embedded template, we have:

    {exp:channel:entries channel="all_events" search:cf_event_id="={embed:embedded_event_id}" dynamic="no" category="8"}
    
    {if no_results}
     {redirect="events/no-results"}
    {/if}
    
     <li class="listing theatre">
      ...output…
     </li>
    {/exp:channel:entries}

    However, the no_results output in the embedded template seems to trump all results, so the redirect happens even when there is content to output. Removing the no_results tag shows the output correctly, but obviously then if there are no results, we don’t get our message displayed about no events.

    The category is right, and everything seems to work, except the no_results tag. I read somewhere that there was a bug with no_results in embedded templates, so to place a no_results tag in the parent, which I’ve tried but with no success.

    Am I missing something here?

  • #2 / Jul 23, 2012 7:22pm

    Kevin Smith

    4784 posts

    Hi Andrew,

    The problem is that the redirect variable gets parsed before the Channel Entries tag is parsed, so it’s not subject to the if no results conditional. I actually wrote a plugin some time back to help tackle this sort of situation (and provide some more options for redirecting), and you can find that add-on here. I think that should take care of it.

  • #3 / Jul 29, 2012 11:50am

    Andrew Armitage

    86 posts

    Thanks Kevin,

    I’m a bit confused - I’ve installed the plug in and it doesn’t seem to have made a difference.

    The problem is that the redirect variable gets parsed before the Channel Entries tag is parsed, so it’s not subject to the if no results conditional.

    This is the extract from the docs on the no_results conditional:

    Further, you may specify that another Template be shown in a case when there are no results. In order to do that, you must use the redirect= variable

    {if no_results} {redirect="channel/noresult"} {/if}

    So why would the redirect be parsed before the channel entries tag? Is it because the template’s embedded?

  • #4 / Jul 30, 2012 4:29pm

    Dan Decker

    7338 posts

    Hi Andrew,

    Let’s take a step back…

    You are trying to build a list of events, correct?
    On days there are no events, you want to display “No events” or some messaging?
    Otherwise, display the event information?

    I think the problem is iteration. Query module returns a result, that result is passed to the embed, and the entry is retrieved. Repeat until there is “no result” - hit the brakes - you have directed EE to redirect! So it does, and that action “wins” and the iteration is stopped.

    For a quick test, replace your redirect with a simple “No Events” message:

    {if no_results}
     No Events
    
    {/if}

    Let me know if I got it all wrong.

    Cheers,

  • #5 / Jul 31, 2012 7:52am

    Andrew Armitage

    86 posts

    Thanks Dan, you’re spot on. I’ve got a list of ‘no events’ Any thoughts on how to approach this?

  • #6 / Jul 31, 2012 5:53pm

    Shane Eckert

    7174 posts

    Hey Andrew Armitage,

    Can you show me your code now? Where is this in the loop? It sounds like the loop is going through the no results several times.

    Cheers,

  • #7 / Jul 31, 2012 5:58pm

    Andrew Armitage

    86 posts

    Hey Shane,

    This is probably a good example, as its showing the output when there are results, but also shows the repeated no_results output. You’re right - it is looping around several times.

    http://www.breweryarts.co.uk/comedy/this-month

  • #8 / Aug 01, 2012 12:15pm

    Shane Eckert

    7174 posts

    Hey Andrew,

    Can you show me the code from the template? I appreciate the example, but would love to see the code before it’s rendered.

    Thank you,

  • #9 / Aug 02, 2012 9:07am

    Andrew Armitage

    86 posts

    The code is the same as it is in the first post in the thread 😊

  • #10 / Aug 02, 2012 11:47am

    Dan Decker

    7338 posts

    Hi Andrew,

    Now that we have the redirect issue sorted out, let’s get you exactly the output you want.

    What do you want to happen in the event of “no results”? What’s the output you want to see on the page?

    It will repeat for each time the embed is called, but we should be able to limit it if that’s what you want.

    I look forward to your reply!

    Cheers,

  • #11 / Aug 02, 2012 6:46pm

    Andrew Armitage

    86 posts

    We have a common template for a no results page, so if we can use that through a redirect or similar, perfect! Failing that - we can create a global variable with this text that simply displays on the expected results page.

    So yes, we’d also need to limit this so it only shows once.

    The trick would seem to be that if there is at least one result found in the embedded template as it loops around, we don’t show any of the no_results content, because we have at least 1 event to show details of.

    Does that help?

    Appreciate your help guys!

  • #12 / Aug 03, 2012 3:34pm

    Dan Decker

    7338 posts

    Hey Andrew,

    I really want to get you the best possible answer, but I need to get a clear picture of how this is all fits together.

    The problem is again in the iteration. There is no way to limit with the current code because:
    The Query module is going to return a list of eventIDs.

    That list is going to be iterated through the embed call and either return an entry or no result.

    The Query will output regardless of the state of the embed.

    You want to control for the instance that an event may occur twice on the same day, how does eventID do that for you? Best practices would have us using only channel entries, if at all possible. I really do want you to have the exact output you are after 😊

    Cheers,

  • #13 / Aug 05, 2012 6:23pm

    Andrew Armitage

    86 posts

    Hey Dan.

    Let me broaden the picture a little. This site uses a third party box office system which provides an XML feed of events, and then instances. Events are a container for event instances, so a single event (title, description etc.) can have several instances (performance times, venue etc.).

    In EE we have an events channel and an instances channel, which uses Andrew Weaver’s DataGrab plugin to pull through from the API into the respective channels. Obviously both the instance and event information have a custom ID, but it is the Event ID which is present (and therefore allows us to connect events with the correct instances) in both that we’re targeting here. This is why we can’t use the native entry_id, but have to search the custom fields for the embedded_event_id which is also unique.

    So using the code at the start of the thread, we want to find all events happening in the next month, but if an event has more than one instance, ignore anything beyond the first instance, because we’re actually going to provide a link to a detail and booking page where visitors can see all instances anyway. Then move on to the next event.

    If there are no events, ideally we’d like to move on to a different page, but if this isn’t possible, output a ‘no events’ message.

    Does this help?

  • #14 / Aug 06, 2012 3:08pm

    Dan Decker

    7338 posts

    Hi Andrew,

    Thanks, that really did help. I just didn’t want to assume too much!

    Have you thought about relationships? I’m not sure how DataGrab operates on relationship fields, but I would think it could build them based on the Event ID. Each instance would relate back to its event.

    Then we could build the channel entries tag and limit the number of returned related entries for each event entry.

    The only other option I could think would be a custom plugin that does the query work and then return a list of relevant entry IDs for a channel entries tag.

    Let me know what you think!

    Cheers,

  • #15 / Aug 14, 2012 5:25am

    Andrew Armitage

    86 posts

    Hey Dan, thanks for your reply.

    My first thought was this is an inspired idea, and why hadn’t I thought of using a relationship! However, having added in the relationship to the event instances channel (relating to the events channel) I don’t see how I can make this work.

    The events data always gets pulled in before the events instances to ensure that there are no orphaned instances. But, when we pull in the instances XML, there is nothing to tie it back to the entry_id of the event, so if we could build the relationship on the Event ID, rather than the entry_id, then I think this would work.

    Have I missed something on the relationship field type?

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

ExpressionEngine News!

#eecms, #events, #releases