We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23247
    • 6 Posts
    Hi,

    I would like to add up the values of a template variable field for several documents. So for example, if I have 2 documents and each has a TV called "Score", I want to sum up the total of both documents’ Scores. So if doc 1 has a score of 100 and doc 2 has a score of 50, I want to have a returned value of 150.

    I tried doing this with getResources, but I couldn’t figure it out.

    Any assistance is much appreciated.

    BTW, I am using Revo 2.0.8.

    Many thanks...
      • 3749
      • 24,544 Posts
      The getResourceField snippet will get the values of the TVs, but I think you’ll have to add them in a custom snippet of your own. It would go something like this (to add the scores from resource 12 and resource 15) -- assumes that the TV is names "score":


      <p>Total: [[!AddScores? &score1=`[[!getResourceField? &id=`12` &field=`score` isTV=`1` ]]` &score2=`[[!getResourceField? &id=`15` &field=`score` isTV=`1` ]]`]]</p>


      <?php
      
      /* AddScores snippet */
      
      return $score1 + $score2;


      If you need to add the scores from a bunch of different resources, you’d probably want to send their IDs in a comma-delimited list to the snippet:

      [[!AddScores? &ids=`12,15,33,45`]]


      The snippet would then look like this:

      <?php
      /* AddScores snippet */
      
      $tvId = 1; // put the id of the TV here
      $docs = explode(',', $ids);
      
      $score = 0;
      
      foreach ($docs as $doc) {
         $tv = $modx->getObject('modTemplateVar', $tvId);
         $score += $tv->getValue($doc);
      }
      
      return $score;
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 23247
        • 6 Posts
        This is great! Thank you. I got the first example working and it is awesome, but I couldn’t get the second example working for some reason.

        I am trying to add the values contained in the TV score contained in docs 23 to 25. Here is my snippet call:
        [[!AddScores? $ids=`23,24,25`]]

        Here is my AddScores snippet

        <?php
        /* AddScores snippet */

        $tvId = 6; // 6 is the id of my TV called score
        $docs = explode(’,’, $ids);

        $score = 0;

        foreach ($docs as $doc) {
        $tv = $modx->getObject(’modTemplateVar’, $tvId);
        $score .= $tv->getValue($doc);
        }

        return $score;

        _____

        It ends up just displaying 0 all the time.

        Thanks again!
          • 3749
          • 24,544 Posts
          Quote from: philiptsang at Mar 28, 2011, 05:25 PM

          [[!AddScores? $ids=`23,24,25`]]

          My bad. That should be &ids, not $ids.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 23247
            • 6 Posts
            Actually, it is my bad...my typo!

            It is working now...in a way. Its kinda funny...I have values of 100, 200, and 300 in documents 23, 24, and 25. It outputs the result as 0100200300 (putting together all the values together rather than adding them up).

              • 3749
              • 24,544 Posts
              Try this:

              $score += (int) $tv->getValue($doc);
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 27716
                • 126 Posts
                Hi Guys,

                I am trying to use this add scores snippet but want to add scores from documents created by certain users.

                Is there an easy way I could use something like the getresources Where / created by functionality with this addscores snippet?

                Stuart
                  • 3749
                  • 24,544 Posts
                  Can you give some more details on exactly what you’re trying to do and why? Things like this:

                  What version of MODX?

                  Do the users change or are they always the same?

                  How are the users selected?

                  Do you want to process all docs created by each user or just some of them?

                  Do you want to add scores from different users together?

                  etc.
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 27716
                    • 126 Posts
                    Hi Bob,

                    Thanks for your response,

                    Im on Modx 2.0.8 pl

                    The count is for user channels on a video site http://bit.ly/dYKTTf

                    The users are static. When I use getResources to display videos for the channel, I just show all created by the user number.

                    Each video has a TV with views.

                    I want to process all docs created by a user under one parent. (/watch)

                    At the moment I only need to add scores from individual users, no cross over between users.

                    Let me know what you think.

                    Stuart
                      • 3749
                      • 24,544 Posts
                      I’m still not sure I understand, but getResources returns text, not resources or data so I don’t think you’ll be able to do math on what it returns.

                      I think you probably will need a custom snippet to get the resources and do the math.

                      Try posting a tag for the imaginary snippet that will do what you want, containing all the information it needs. Then maybe we can suggest an approach.
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting