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

    This snippet call from the AddScores above is close,

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


    The only difference is, I need to populate the &ids with a list of ids of all the documents created by a certain user.

    The template variable is just videoViews. The out Put will go under Views on the channel page http://thailand.tv/tigermuaythai

    To populate the videos on the page I us &where=`{"createdby:=":61}` from getresources. So i need to sum the template variables from the exact same documents.

    Let me know what you think.

    Stuart
      • 4172
      • 5,888 Posts
      you can try this:

      create a snippet ’addScore’ with:

      <?php
      $sum = $modx->getPlaceholder('scores_sum');
      $sum = !empty($sum) ? $sum+$score : $score; 
      $modx->setPlaceholder('scores_sum',$sum);


      in your getResources - tpl place:

      [[!addScore? &score=`[[+yourScoreTv]]`]]


      and where you want to output the sum place:
      [[+scores_sum]]
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 3749
        • 24,544 Posts
        Are you displaying the total views of all pages created by the person viewing the page?
          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 Bruno,

          I tried that setup but no Joy. Will investigate further though and see if I can get to work.

          Bob, I am displaying the total video views of the videos created by certain users.

          Same like a YouTube channel, you see total video views in the channel.

          Let me know your thoughts.

          Im thinking it would be good if I could just pull all of the document IDs created by a certain users and populate this snippet with it.

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

          Stuart
            • 3749
            • 24,544 Posts
            I think you want something like this (the snippet tag will contain the IDs of the users):

            <?php
            /* AddScores snippet */
            
            $tvId = 1; // put the id of the TV here
            $output = '';
            
            $userIds = explode(',', $ids);
            $tv = $modx->getObject('modTemplateVar', $tvId);
            
            foreach ($userIds as $userId) {
                $pages = array();
                $userObj = $modx->getObject('modUser', $userId);
                if ($userObj) {
                    $pages = $userObj->getMany('CreatedResources');
                    $score = 0;
                     if (!empty($pages)) {
                         foreach($pages as $page)  {
                             $score += (int) $tv->getValue($page->get('id'));
                         }
                         $output .= '<p>User: ' . $userObj->get('username') . ' . . . Views: ' . $score . '</p>';
                     }
                }
            }
            
            return $output;
            


            I’m sure there is a more efficient (but more complicated) method, since this pulls a lot of information you don’t need, but it should work.
              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
              Hey Bob,

              Thanks for this.

              It worked perfectly!

              I added this line so it determines the usser from their channel page.
              $ids = $modx->documentObject[’createdby’];

              Starting to study more PHP and hopefully will be writing snippets myself one day soon!
                • 22303 MODX Staff
                • 10,725 Posts
                Quote from: smcilreavy at May 03, 2011, 05:22 AM

                I added this line so it determines the usser from their channel page.
                $ids = $modx->documentObject[’createdby’];
                FYI, you should use
                $modx->resource->get('createdby');
                as the documentObject variable was deprecated in 2.0 and no longer available in 2.1+.
                  • 3749
                  • 24,544 Posts
                  Could you post your code?
                    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
                    <?php
                    /* AddScores snippet */
                    
                    $tvId = 8; // put the id of the TV here
                    $output = '';
                    $ids = $modx->documentObject['createdby'];
                    
                    $userIds = explode(',', $ids);
                    $tv = $modx->getObject('modTemplateVar', $tvId);
                    
                    foreach ($userIds as $userId) {
                        $pages = array();
                        $userObj = $modx->getObject('modUser', $userId);
                        if ($userObj) {
                            $pages = $userObj->getMany('CreatedResources');
                            $score = 0;
                             if (!empty($pages)) {
                                 foreach($pages as $page)  {
                                     $score += (int) $tv->getValue($page->get('id'));
                                 }
                                 $output .= ' ' . $score . '';
                             }
                        }
                    }
                    
                    return $output;


                    Then on each channel page I call it with
                    [[!addScores?]]


                    As that page is created by the user, it finds all of their videos and adds.

                    Thanks for the info opengeek. Changed and working
                      • 3749
                      • 24,544 Posts
                      I suspected as much. You’re only getting the pages for one user, not a collection of users, so the code I wrote is more complex than it needs to be. Try this:

                      <?php
                      /* AddScores snippet */
                      
                      $tvId = 8; // put the id of the TV here
                      $output = '';
                      $UserObj = $modx->resource->getOne('CreatedBy');
                      $tv = $modx->getObject('modTemplateVar', $tvId);
                      
                      $pages = $userObj->getMany('CreatedResources');
                      $score = 0;
                      
                      foreach($pages as $page)  {
                           $score += (int) $tv->getValue($page->get('id'));
                      }
                      $output .= ' ' . $score . '';
                      return $output;


                      No loop is necessary except the one to add up the data from the pages and I left out the sanity checks because we can assume that the current page has a creator and that that user has created at least one page (the current one).

                        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