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 Guys,

    I have a snippet I want to loop with an array of document Ids. I want to specify...say 100 Doc id’s manually and have the snippet loop through all of those documents.


    This first snippet gets a value from a CSV parser and puts the value in a Template variable. I want to loop this snippet across 100 documents. This snippet is working.
    <?php
    $vidId = isset($vidId)? $vidId : '';
    
    $output = $modx->runSnippet('updateViews',array(
       'url' => 'http://domain.com/'.$vidId.'.flv'
    ));
    $tv = $modx->getObject('modTemplateVar',array('name'=>'vidViewCount'));
    $tv->setValue($vidId, $output);
    $tv->save();



    I was trying to make this second snippet the loop but this is where it is failing.

    <?php
    $arr = array(13, 14, 15);
    foreach ($arr as $arrItem) {
     $modx->runSnippet('executeViewsUpdate',array(
       'vidId' => '$arrItem'));
    }


    I intend to execute this second snippet via cronmanager every 24 hours to update a list of view counts.

    Any guidance on how to achieve this appreciated.

    Best Regards,

    Stuart


      • 33968
      • 863 Posts
      Hi Stuart

      I think you would be better off using getCollection on ’modTemplateVarResource’ (not tested):
      <?php
      $vidIds = isset($vidIds) ? $vidIds : '';    // comma separated list of ids
      $vidIds = explode(',',$vidIds);  // convert to array
      
      $c = $modx->newQuery('modTemplateVarResource');
      $c->where(array(
          'tmplvarid'=>'6',   // replace this with the id of your 'vidViewCount' tv
          'contentid:IN' => $vidIds,
      ));
      
      $tvs = $modx->getCollection('modTemplateVarResource',$c);
      
      foreach ($tvs as $tv) {
          $value = $modx->runSnippet('updateViews',array(
                  'url' => 'http://domain.com/'.$tv->get('contentid').'.flv'
          ));
          $tv->set('value', $value);
          $tv->save();
      }
      


      It might also be more efficient to include the ’updateViews’ code within this snippet rather than calling runSnippet 100 times in a loop, or alternatively call it once at the end and have it accept the $vidIds array.

      Hope that helps!
        • 27716
        • 126 Posts
        Hi Lucas,

        Thanks for the code.

        It works, but like some of the other solutions I tried it fails when I try multiple docs.

        Do you have an hourly rate or something where you could help finalize this. It is driving me up the wall.

        I have limited budget but if you could quote a price to look at the csv parsing snippet and get it working so I can pass multiple IDs, update the TVs and get it working as a cron it would be appreciated.

        Stuart
          • 3749
          • 24,544 Posts
          Try this:



          <?php
          $arr = array(13, 14, 15);
          foreach ($arr as $key => $arrItem) {
           $modx->runSnippet('executeViewsUpdate',array(
             'vidId' => '$arrItem'));
          }
            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,

            Thank you for the code...

            Spent some time working with it but still encountering errors.

            I think the error is happening somewhere in the process of Parsing the CSV
            http://modxcms.com/forums/index.php/topic,64888.msg370838.html#msg370838

            Made a lot of progress considering I couldn’t code snippets at all a few weeks ago...but now...over this one.

            If i have to keep working on it...they are going to have to put me in one of those comfy white jackets and send me to rest in a room with soft walls smiley

            Lucas, PM me and let me know if you can take a look.

            Stuart
              • 3749
              • 24,544 Posts
              Right. On second thought, your code should have worked.

              In your working example, you’re sending the ’url’ argument to executeViewsUpdate(). In the non-working one, you’re sending ’vidId’.




                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,

                A lot of the snippet labels etc changed as I was working on it, but when testing I made sure everything was correct. I don’t think it was the issue, because I could get it working perfectly for a single video, but any time it had to execute for multiple IDs it would fail.

                I dont really know what happens with the CSV parser, maybe sending multiple requests to it is making it fall over...

                But its out of my hands now........Thank God smiley

                Appreciate your assistance.

                  • 18373 ☆ A M B ☆
                  • 3,141 Posts
                     'vidId' => '$arrItem'));


                  That will send the string $arrItem to the vidId parameter, and not the value as you were probably looking for.

                  So either get rid of the quotes:
                     'vidId' => $arrItem));

                  or use double quotes which will parse variables
                     'vidId' => "$arrItem"));


                  http://v1.jeroenmulder.com/weblog/2005/04/php_single_and_double_quotes.php
                    Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                    Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                    • 27716
                    • 126 Posts
                    Hi Mark,

                    Thanks so much for the feedback.

                    I did get the impression something like that was happening as I would often test just returning a variable, and often it would send an array of numbers back.

                    Good to know thats where I was going wrong.

                    Regards,

                    Stuart