We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36594
    • 11 Posts
    Hope someone can help with this. I have a snippet that the number of facebook likes for a url. This outputs as a number i.e. '1'. How can I get ditto to order child documents based on these numbers? I have tried placing the snippet call in a TV and ordering by that TV, but it doesn't work.


    Snippet:
    <?php
    $source_url = $modx->makeUrl($modx->documentIdentifier, '', '', 'full');
    
    $url = "http://api.facebook.com/restserver.php?method=links.getStats&urls=".urlencode($source_url);
    $xml = file_get_contents($url);
    $xml = simplexml_load_string($xml);
    $shares = $xml->link_stat->share_count;
    $likes = $xml->link_stat->like_count;
    $comments = $xml->link_stat->comment_count;
    $total = $xml->link_stat->total_count;
    $max = max($shares,$likes,$comments);
    
    echo $likes;
    ?>


      • 3749
      • 24,544 Posts
      The problem is that your snippet always gets the likes for the current page (the one with the Ditto tag on it).

      I think you might be able to do it with an @EVAL TV with runSnippet if the snippet just returned number of likes for that individual resource.

      If not, the only thing I can think of is a plugin connected to OnWebPageRender that gets the "likes" for that page and writes them into the TV. Then the TV could be used to sort them, but the numbers would not be current except for recently visited pages.

      I can't think of any other way to do it without either hacking the Ditto code or replacing Ditto with your own custom snippet that calculated the likes for all pages and then sorted them.

      Maybe someone else has a better idea.
        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
        • 36594
        • 11 Posts
        I haven't used @EVAL before, do you know of a tutorial?
          • 36594
          • 11 Posts
          Got @EVAL working. Unfortunately it didn't work. Thanks for your input.
            • 3749
            • 24,544 Posts
            I don't see any reason why it shouldn't work with @EVAL. Change 'echo' to 'return'.

            You'll also have to send the correct Resource ID as an argument to your snippet in the runSnippet() call instead of using documentIdentifier.

              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
              • 36594
              • 11 Posts
              Ok I have changed 'echo' to be 'return', still the same result.

              Do you mean I also need to change the code like this?

              $source_url = $modx->makeUrl($modx->documentIdentifier, '', '', 'full');
              


              to

              $source_url = $modx->makeUrl($modx->runSnippet(), '', '', 'full');



              My EVAL code is as follows:
              @EVAL return $modx->runSnippet('get_like_count');
                • 3749
                • 24,544 Posts
                I meant to use something like this:

                @EVAL return $modx->runSnippet('get_like_count', array('docId'=>'12'));


                Then in the snippet:

                <?php
                $source_url = $modx->makeUrl($scriptProperties['docId'], '', '', 'full');


                The rest of the snippet would look like your original one (but with return instead of echo).
                  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
                  • 36594
                  • 11 Posts
                  I seem to be getting a parse error


                  MODx encountered the following error while attempting to parse the requested resource:
                  « `` is not numeric and may not be passed to makeUrl() »
                    • 36594
                    • 11 Posts
                    I changed the line

                    $source_url = $modx->makeUrl($scriptProperties['docId'], '', '', 'full');


                    to

                    $source_url = $modx->makeUrl($docId, '', '', 'full');


                    and now it gets the like numbers again but still not ordering correctly using script. I have tried putting numbers 0,1,2 into my TV's (for three child documents) and ditto is sorting properly, so ditto still seems to be running before script is parsed.
                      • 36594
                      • 11 Posts
                      Anyone got any further suggestions?