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 is the purpose of {total_results}?

September 25, 2011 8:04pm

Subscribe [4]
  • #1 / Sep 25, 2011 8:04pm

    grantmx

    1439 posts

    Ok, this has always bugged me and hopefully someone can chime in on this.

    What is the purpose of the {total_results} variable when it loops the total results (or total count) for each result?

    For example, if I do something like:

    {exp:channel:entries channel="something" author_id="CURRENT_USER"}
    {total_results}
    {/exp:channel:entries}

    and If I have lets say 8 posts in channel ‘something’, It will output something like:

    8
    8
    8
    8
    8
    8
    8
    8

    But If I limit the result to 1:

    {exp:channel:entries channel="something" author_id="CURRENT_USER" limit="1"} 
    {total_results}
    {/exp:channel:entries}

    It outputs:

    1

    Am I missing something here? :/  Why does it loop the total number of entries I have with the total number of entries I have, yet when I limit to 1 it totals it as 1?

    So, the question then is, why does this even exist since it never does what we intrinsically want it to do? Why does it not just give me a single number with the total number of results for ‘something’ channel?

     

  • #2 / Sep 26, 2011 11:16am

    Rob Sanchez

    335 posts

    It’s useful when you want to see if you’re on the last entry of the loop:

    {exp:channel:entries channel="products"}
    {if count == 1}
    <ul>
    {/if}
    <li>{title}</li>
    {if count == total_results}
    </ul>
    {/if}
    {/exp:channel:entries}

    Also, I’ve made an extension which adds an {absolute_total_results} tag to the entries: https://github.com/rsanchez/absolute_total_results

  • #3 / Sep 26, 2011 12:25pm

    grantmx

    1439 posts

    Hey Rob - I saw your extension, but even with the above scenario I mentioned I got no results.  Just want a simple single output of the total results.

  • #4 / Sep 26, 2011 12:27pm

    Rob Sanchez

    335 posts

    I see what you’re saying, all you wanna do is output the total results once. Forget about my extension, then, I misunderstood you the first time.

    You could just do this:

    {exp:channel:entries channel="products"}
    {if count == 1}{total_results}{/if}
    {/exp:channel:entries}
  • #5 / Sep 26, 2011 1:13pm

    grantmx

    1439 posts

    Word?  Interesting.  Now why does that work?  Is it because the conditional stops the loop and evaluates just one result?

  • #6 / Sep 26, 2011 1:14pm

    Rob Sanchez

    335 posts

    It doesn’t technically stop the loop, but rather, only prints text during the first run of the loop.

  • #7 / Sep 26, 2011 1:24pm

    grantmx

    1439 posts

    Gotcha.  Thanks Rob.

  • #8 / Sep 27, 2011 2:04pm

    Mark Bowen

    12637 posts

    Hi Marshall,

    I’m not entirely certain what you’re doing here so this may or may not be of relevance but if you say had more than 100 entries in your Channel and used the above code then you might see a performance hit although not too noticeable with just one tag due to the fact that ExpressionEngine would bring back internally all 101 (for example) entries just to give you what you need here.

    If you used these types of conditionals a lot in your templates then those slight hits could possibly add up to a larger impact. I’m not saying that you will have problems here but definitely just something to be made aware of.

    If that did start to happen then you might be better off using the Query Module tag and getting what you need in that way instead :

    Pseudo code

    {exp:query sql="
    
    SELECT COUNT(*) AS channel_total_results
    FROM
            exp_channel_titles
    WHERE
            channel_id = '1'
    AND
            status = 'open'
    "}
    
    {channel_total_results}
    
    {/exp:query}

    Mark

    P.S. Thanks for the help there Rob.

  • #9 / Sep 27, 2011 3:04pm

    grantmx

    1439 posts

    Thanks Mark - I had tried to use a query and had even got results, but I couldn’t figure out how to limit the query to just the CURRENT_USER of the entry since that isn’t an column in exp_channel_titles .  My SQL skills aren’t up to par yet, so I ditched the idea.

    But it stands to reason that intrinsically a tag like that should just give me the total results and nothing else, no?

  • #10 / Nov 15, 2011 11:33am

    Oskar Smith

    33 posts

    A quick addition while it’s on my mind - I used Rob’s extension but also needed to show the {absolute_total_results} within the {paginate}{/paginate} tagpair. You can do this by modifying his extension by adding an extra hook:

    $hooks[] = array(
       'method' => 'channel_entries_query_result',
       'hook' => 'channel_entries_query_result'
      );

    And corresponding extra method:

    public function channel_entries_query_result($channel, $query_result) {
             $channel->paginate_data = $this->EE->TMPL->swap_var_single('absolute_total_results', $channel->total_rows, $channel->paginate_data);
             return $query_result;
            }

    It doesn’t actually modify the query result - the only reason I used this hook was because it appeared to come at the right place in the code for us to be able to access the total_results and paginate_data variables.

    Hope this proves useful for someone. Rob - feel free to include in the extension if you think it would be worthwhile and isn’t too hack-y….

  • #11 / Nov 16, 2011 11:24am

    Oskar Smith

    33 posts

    Rob’s extension now includes the above modification.

    https://github.com/rsanchez/absolute_total_results

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

ExpressionEngine News!

#eecms, #events, #releases