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.

Simple Query Issue

December 05, 2011 11:35am

Subscribe [3]
  • #1 / Dec 05, 2011 11:35am

    JohnRuben

    5 posts

    I have a simple problem, but am finding it hard to figure out how to execute it in EE.

    I have a table of scholarship winners spanning multiple years, with the year existing in the Year channel field.  I have the URL established so that a user can specify /scholarship-winners/ and get the most recent year’s scholarship winners, while an optional parameter can be passed in as /scholarship-winners/2009/ and only 2009 winners will appear.

    If the parameter passed in does not match any records, I just want to display a simple No results found. without the table and layout for rendering actual scholarship winners.

    I can’t seem to figure out how to lay this out in EE, though.  What’s the best way to just test to see if channel records exist based on a channel field value, if they exist render a specific kind of layout, and if not display a different kind of layout?  Seems easy enough, but I’m not sure how to do it.  Both the query module and the channel entries module will iterate over the found set, so I don’t want my successful query to display the scholarship winners 100 times if there are 100 winners.  I just need a simple conditional test, like

    if entries_exist
      display table
    if:else
      display error dialog
    /if

    Thanks, in advance, for any assistance!

  • #2 / Dec 05, 2011 12:09pm

    glenndavisgroup

    436 posts

    Hi JohnRuben,

    Can you post your code please?

    Thank you,

    Mike

  • #3 / Dec 05, 2011 12:18pm

    glenndavisgroup

    436 posts

    Put the following anywhere in your tag pair:

    {if no_results}
    No results found. Please try again.</h1>
    {/if}

    I hope that helps.

    Mike

  • #4 / Dec 05, 2011 12:40pm

    JohnRuben

    5 posts

    <h2>Scholarship Recipients</h2>
    <p> <br />
    <table id="scholarship_winners"><br />
     <thead><br />
      <th>Student Name</th><br />
      <th>Parent Name</th><br />
      <th>Parent Hotel/Concession/Club</th><br />
      <th>Parent Job Title</th><br />
      <th>High School</th><br />
      <th>College/University</th><br />
      <th>Career Choice</th><br />
     </thead></p>
    
    <p> <tbody><br />
     {exp:channel:entries channel="scholarship_winner" dynamic="no" status="open" search:schol_year="{segment_3}" orderby="schol_last_name|schol_first_name" sort="asc|asc"}<br />
      <tr><br />
       <td><a href="#{url_title}class=dark">{title}</a></td><br />
       <td>{schol_parent_full_name}</td><br />
       <td>{schol_parent_shop}</td><br />
       <td>{schol_parent_job_title}</td><br />
       <td>{schol_high_school}</td><br />
       <td>{schol_college}</td><br />
       <td>{schol_career_choice}</td><br />
      </tr><br />
     {/exp:channel:entries}<br />
     </thead><br />
    </table>

  • #5 / Dec 05, 2011 12:43pm

    JohnRuben

    5 posts

    To glenndavisgroup,

    I thought about the no_results approach, but that doesn’t work for my setup, since I have the channels query wrapped around a table as such:

    <table>
    <thead>
    </thead>
    
    <tbody>
    {entries query}
     <tr>
     </tr>
    {/entries query}
    </tbody>
    </table>

    I don’t want the table formatting at all if there are no results, but instead only a No Results Found.

  • #6 / Dec 05, 2011 1:19pm

    glenndavisgroup

    436 posts

    Hi JohnRuben,

    I’m kind of confused. Your code shows the layout like so:

    <table>
      <tr>
       <td>...</td>
       ....
      </tr>
     {exp:channel:entries ...}
      <tr>
       <td>...</td>
       ...
      </tr>
     {/exp:channel:entries}
     </thead>
    </table>

    But in your last comment you mentioned:

    “I don’t want the table formatting at all if there are no results, but instead only a No Results Found.”

    The layout above will not work. You will need to move everything in side of the channel entries tag pair like so:

    {exp:channel:entries ...}
    {if count == 1}
    <table>
      <tr>
       <td>...</td>
       ....
      </tr>
    {/if}
    
      <tr>
       <td>...</td>
       ...
      </tr>
    
    {if count >= total_results}
     </thead>
    </table>
    {/if}
    
    {if no_results}
    No results found. Please try again.
    {/if}
    
    {/exp:channel:entries}

    Sorry I didn’t test the code above but I think something like that should work for you. I hope that helps.

    Mike

  • #7 / Dec 05, 2011 1:32pm

    JohnRuben

    5 posts

    Ahhhh.  I hadn’t thought about the method of using the {exp:channel:entries} loop, but put in surrounding content using count == 1.  That seems to be working, and is the trick for what I’m trying to achieve.  Thanks for all the help, folks!

  • #8 / Dec 05, 2011 1:58pm

    glenndavisgroup

    436 posts

    I’m glad that worked for you 😊

    Cheers,

    Mike

  • #9 / Dec 06, 2011 10:11am

    Kevin Smith

    4784 posts

    Thanks for your help on this one, Mike! And I’m glad to hear you’re all squared away, John. Let me know if there’s anything else I can do to help.

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

ExpressionEngine News!

#eecms, #events, #releases