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.

Count entries using custom field

November 20, 2010 9:47am

Subscribe [2]
  • #1 / Nov 20, 2010 9:47am

    okenathan

    42 posts

    I’m using a custom field to link entries. I wrote a simple sql query to count the related entries.

    {exp:query sql="
    SELECT cd.entry_id, cd.field_id_1, ct.author_id, ct.title, ct.url_title, ct.view_count_one, ct.year, ct.month, ct.day, m.member_id, m.screen_name
    FROM 
    exp_channel_data cd 
    
    INNER JOIN 
    exp_channel_titles ct
    USING(entry_id)
    LEFT JOIN exp_members  m ON m.member_id = ct.author_id
    WHERE field_id_4= '{embed:graankorrel}' 
    AND entry_id <> '{embed:entry_id}'
    ORDER BY 'view_count_one' DESC
    "}

    The query works perfectly. All i want is to show how many related entries have been written. I want to count them.

    I thought using the

    {total_results}

    would just give me that; the total results. I used it like this:

    {if no_results}0 results{/if}    
    
    {if total_results == "1"} 
    
    {total_results} result
    
    {if:else}
    
     {total_results} results
    
    {/if}

    This works really well, until more than one (1) related entries are found. I get double (or triple) results. Like this:

    2 results 2 results

    Of course this is exactly what the query module should do, but it’s not what i would like to see.

    I don’t want EE to loop through the results as this is only generating repeating code. I searched a lot but am not sure if there is a way of removing all results (in the EE part) except the first..

    So my question is: Is there a way i can remove all the results (in the looping EE part) except the first?

    [Mod Edit: Moved to the CodeShare Corner forum]

  • #2 / Nov 21, 2010 11:26am

    e-man

    1816 posts

    How’s this?

    {if no_results}0 results{/if}    
    
    {if total_results == "1"} {total_results} result {/if}
    
    {if total_results > "1" && count == total_results} 
        {total_results} results 
    {/if}
  • #3 / Nov 21, 2010 3:43pm

    okenathan

    42 posts

    Thanks e-man. 😊

    This is exactly what I was looking for.
    Problem solved. Now trying to understand why EE isn’t looping anymore.

    Am i correct in assuming that the “count” option is indeed the “result counter”?

    So comparing the count with the total results somehow ends the loop.

    Or is it the “if more results than 1” code that generates a single output?

    Thanks again.

  • #4 / Nov 21, 2010 3:51pm

    e-man

    1816 posts

    The count variable is indeed a counter, so when count == total_results it means you have reached the final result.
    You’re still looping through the results but only displaying it when you’ve reached the final result, which can only happen once.
    Glad it helped 😊

  • #5 / Nov 24, 2010 5:42pm

    okenathan

    42 posts

    Thanks. It’s nice to understand the code.

    Even though it’s strange how difficult it seemed before you showed me the (now obvious) solution. 😊

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

ExpressionEngine News!

#eecms, #events, #releases