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.

Add (mathematical) two custom number fields together, possible?

October 04, 2010 10:36am

Subscribe [3]
  • #1 / Oct 04, 2010 10:36am

    Brad Morse

    428 posts

    I have two fields, that are set as numbers, in EE 2

    I want to add and display the total.

    I know I can accomplish this with php, but is it possible to accomplish this natively in EE?

    Below is the code that accomplishes this using php

    <?="{custom_number_field_1}" + "{custom_number_field_2}"?>

    Is it possible to do this natively in EE 2?

  • #2 / Oct 04, 2010 7:48pm

    I think MX Calculator may do the job.

  • #3 / Oct 04, 2010 10:56pm

    Brad K Morse

    178 posts

    Perfect, thanks!

    {basketball_schedule_score}
        {if "{count}" == 1}
        <table class="basketball-result">
        <tr>
            <th>Team</th>
            <th>1st</th>
            <th>2nd</th>
            <th>Total</th>
        </tr>
        {/if}
        <tr>
            <td>Wolverines</td>
            <td>{first_half_you}</td>
            <td>{second_half_you}</td>
            {if "{exp:mx_calc expression='sum({first_half_opponent},{second_half_opponent})'}" < "{exp:mx_calc expression='sum({first_half_you},{second_half_you})'}"}
            <td class="total">
            {if:else}
            <td>
            {/if}
                {exp:mx_calc expression="sum({first_half_you},{second_half_you})"}
            </td>
        </tr>
        <tr>
            <td>{title}</td>
            <td>{first_half_opponent}</td>
            <td>{second_half_opponent}</td>
            {if "{exp:mx_calc expression='sum({first_half_opponent},{second_half_opponent})'}" > "{exp:mx_calc expression='sum({first_half_you},{second_half_you})'}"}
            <td class="total">
            {if:else}
            <td>
            {/if}
                {exp:mx_calc expression="sum({first_half_opponent},{second_half_opponent})"}
            </td>
        </tr>
    {if "{count}" == "{total_rows}"}</table>{/if}
    {/basketball_schedule_score}

    http://cl.ly/5db9a73634775de69439

  • #4 / Oct 04, 2010 11:27pm

    Lisa Wess

    20502 posts

    Be careful when quoting and bracing conditionals like that, you’re creating a security problem.  See troubleshooting conditionals.

    You’ve got quite a few in there, too.

    Thanks also for sharing your solution. =)

  • #5 / Oct 04, 2010 11:43pm

    Brad K Morse

    178 posts

    You are referring to #5 on that page?

    5) Are you comparing a quoted variable to a value?  This is not supported and depending on the source of the variable, may even have security implications.  By doing this, you are allowing the tag that “owns” that variable to parse and replace it with its unfiltered contents.  Doing so is trusting that it has no bad characters, no javascript, and no need for error checking that is normally performed when the conditional is used with the proper syntax outline above.

  • #6 / Oct 05, 2010 12:06pm

    Lisa Wess

    20502 posts

    Yep, #5. =)

  • #7 / Oct 05, 2010 12:43pm

    Brad Morse

    428 posts

    If I could “guarantee” the value of each field, is only a number and nothing else, then it’d be secure?

  • #8 / Oct 05, 2010 4:55pm

    Lisa Wess

    20502 posts

    Hi, Brad - that’s really something that you have to decide.  If you are sanitizing yourself, or not taking user input, then it may be an acceptable risk.  I’d never do that with something like segment conditionals.

    Moreover, there’s no need at all to do it with this:

    {if "{count}" == "{total_rows}"}</table>{/if}

    =)

  • #9 / Oct 05, 2010 5:04pm

    Brad Morse

    428 posts

    {if "{count}" == "{total_rows}"}</table>{/if}

    is for the matrix field type.

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

ExpressionEngine News!

#eecms, #events, #releases