We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12410
    • 353 Posts
    Guys whats the best way/syntax of adding/multiplying and output values in tvs

    eg

    [[*num1]] + [[*num2]] + [[*num3]] etc....= 1342 (output)
    
    


    Thanks

    Modx Revo 2.2
      • 19369
      • 1,098 Posts
      Try with MIGX
        • 4172
        • 5,888 Posts
        If you want to try it with MIGX.

        This scenario did work for me:

        <table>
        [[getImageList? 
        &tpl=`addvalues`
        &value=`[
        {"MIGX_id":"1","num":"10"},
        {"MIGX_id":"2","num":"5"},
        {"MIGX_id":"3","num":"6"},
        {"MIGX_id":"4","num":"4"}
        ]`]]
        
        <tr><td>Result</td> <td>[[+result_of_num]]</td></tr>
        </table>


        chunk addvalues:
        [[addvalue? &idx=`[[+idx]]` &value=`[[+num]]`&placeholder=`result_of_num`]]
        <tr><td>[[+idx]]</td> <td>[[+num]]</td></tr>
        


        snippet addvalue:
        $oldvalue = $modx->getPlaceholder($placeholder);
        $value = $modx->getOption('value',$scriptProperties,0);
        $newvalue = empty($oldvalue)? $value : $oldvalue + $value;
        $modx->setPlaceholder($placeholder,$newvalue);
        
        return $output=='result' ? $newvalue : '';
        


        So If you would create a MIGX-TV which returns this output:

        [{"MIGX_id":"1","num":"10"},
        {"MIGX_id":"2","num":"5"},
        {"MIGX_id":"3","num":"6"},
        {"MIGX_id":"4","num":"4"}]


        it should work.
        [ed. note: Bruno17 last edited this post 14 years, 6 months ago.]
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 12410
          • 353 Posts
          Thanks - perfect!

          [[getImageList?
          &tvname=`myTV`
          &tpl=`addvalues`
          ]]
          [ed. note: howster last edited this post 14 years, 6 months ago.]
            • 12410
            • 353 Posts
            Ok a bit more complex this time but this is very useful. If i have a migx tv called myTV with 6 numerical fields (textfields) called num1, num2....num6

            I was hoping that I could sum ALL the values using the chunk

            [[addvalue? &idx=`[[+idx]]` &value=`[[+num[[+idx]]]]`&placeholder=`result_of_num`]]
            <tr><td>[[+idx]]</td> <td>[[+num[[+idx]]]]</td></tr>


            It looks like I will need to amend the snippet though. I am working on a front end spreadsheet and this would be perfect for totalling values.
            I can total the columns but cant total the rows or Grand total. Please see attachment
            THANKS
            [ed. note: howster last edited this post 14 years, 6 months ago.]