0
{exp:query sql="SELECT states FROM states"}
    {if count == total_results}
        and {states}
    {if:else}
        {states},<span> </span>
    {/if}
{/exp:query}

Result: Colorado, Kentucky, Missouri, Oklahoma, and Tennessee But I'm expecting Colorado, Kentucky, Missouri, Oklahoma and Tennessee

Any idea?

1
  • To make answers to your questions easier / more accurate, it is helpful if you can include details of which version of EE you are using, and sometimes which version of php the server you are using is running in your answer. Feb 2 at 20:57
1

You can include simple mathematical operators within EE conditionals so you could use something like this:

{exp:query sql="SELECT states FROM states"}
    {states}
    {if count < (total_results - 2)},
    {if:elseif count == (total_results - 1)} and 
    {/if}
{/exp:query}

I have spread out the conditional to make it easier to read, but you might want to concatenate to avoid spurious linefeed / return characters appearing in your output.

HTH

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.