We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27697
    • 11 Posts
    In case this is of use to anyone... I came up with a little snippet to return the most popular pages on a site. If you name the snippet "PopularPages", the usage goes like this:

    [[PopularPages? &numPages=`7`]]

    numPages defaults to 5.

    $username="YOUR-USER-NAME";
    $password="YOUR-PASSWORD";
    $database="YOUR-DATABASE";
    $numPages = (isset($numPages))? $numPages : 5;
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $sql = "SELECT modx_site_content.pagetitle, count(modx_log_access.document), modx_log_access.document FROM modx_log_access INNER JOIN modx_site_content ON modx_log_access.document=modx_site_content.id GROUP BY modx_log_access.document order by count(modx_log_access.document) desc limit $numPages;";
    $result = mysql_query($sql);
    echo "<ul>";
    while($row = mysql_fetch_array($result)) {
      $apage = $row['document'];
      $atitle = $row['pagetitle'];
      echo "<li><a href=\"index.php?id=$apage\">$atitle</a></li>";
    }
    echo "</ul>";
    mysql_close();
      • 22815
      • 1,097 Posts
      Is there any particular reason why you didn’t use the dBAPI? That way you could run a
      Select query without having to set the db user name & password within the snippet.
        No, I don&#39;t know what OpenGeek&#39;s saying half the time either.
        MODx Documentation: The Wiki | My Wiki contributions | Main MODx Documentation
        Forum: Where to post threads about add-ons | Forum Rules
        Like MODx? donate (and/or share your resources)
        Like me? See my Amazon wishlist
        MODx "Most Promising CMS" - so appropriate!
        • 27697
        • 11 Posts
        The particular reason is ignorance. (My forte.)
          • 25663 MODX Staff
          • 12,272 Posts
          LOL
            Ryan Thrash, MODX Co-Founder
            Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
            • 11975
            • 2,542 Posts
            Hi,

            I’ve modified your snippet to use modx api.

            global $modx;
            
            
            //how many item would you like to display
            $numPages = (isset($numPages))? $numPages : 5;
            
            
            $sql = "SELECT modx_site_content.pagetitle, count(modx_log_access.document) AS totalView, modx_log_access.document "
            ."FROM modx_log_access INNER JOIN modx_site_content ON modx_log_access.document=modx_site_content.id "
            ."GROUP BY modx_log_access.document order by totalView desc limit $numPages;";
            
            
            if ($rs= $modx->db->query($sql)) {
            $listPopular = "<ul>";
            $listPopularItem = '';
                //fetch the results
            
            	while ($row= $this->db->getRow($rs)) {		
            	  $apage = $row['document'];
                $atitle = $row['pagetitle'];
                $listPopularItem .= "<li><a href=\"[~".$apage."~]\">$atitle</a></li>";
            	}
            $listPopular .= $listPopularItem."</ul>";
            
            }
            
            return $listPopular;
            
            


            I had to change the sql query because of an an error with GROUP BY statement.

            Hope that helps.

            :-)
              Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
              • 22815
              • 1,097 Posts
              That’s fab. Although it would be better still if it used $modx->dbConfig[’table_prefix’] rather than hardwiring modx_ !!

              If one of you made that change, it would be pretty complete and totally worthy of being added to the Repository:
              http://modxcms.com/downloads.html

              (Whichever one of you adds it, you should add a comment block in the snippet crediting both smartalec and heliotrope.)

              One note of caution: it looks like it will list any hidden pages if they happen to be high-traffic. Maybe that’s something for a future version!

              Anyway, thanks to both of you.
                No, I don&#39;t know what OpenGeek&#39;s saying half the time either.
                MODx Documentation: The Wiki | My Wiki contributions | Main MODx Documentation
                Forum: Where to post threads about add-ons | Forum Rules
                Like MODx? donate (and/or share your resources)
                Like me? See my Amazon wishlist
                MODx "Most Promising CMS" - so appropriate!
                • 11975
                • 2,542 Posts
                I shall make the needed changes (published=1 and privateweb=0) but do I have to check if the field privatemgr = 0
                I guess we could also add parameter such as which field form site_content do you want to display (menu title, page title, ...)

                :-)
                  Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
                  • 34162
                  • 1 Posts
                  Haven’t tried it yet, but I argue that it will not only show hidden but also unpublished pages and pages the current user has no access to.
                  To create a select which respects those aspects would be quite complicated.
                    • 11975
                    • 2,542 Posts
                    I agree with you.
                    This is a major issue though visitor who doesn’t have rigths to access the page and click the link should have to register before viewing the page.
                    It can be considered as an incent to smiley

                    Perhaps this feature(sort by pouplarity) could be added to ditto.

                      Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
                      • 22815
                      • 1,097 Posts
                      Of course, if less people go to the unpublished/secret pages (or if you have no such pages) it isn’t a problem, and for many people this is useful as it is.

                      To be honest, I would advise you guys not to add much more functionality to it as I hear the stats structure is due for a change in 0.9.5.
                        No, I don&#39;t know what OpenGeek&#39;s saying half the time either.
                        MODx Documentation: The Wiki | My Wiki contributions | Main MODx Documentation
                        Forum: Where to post threads about add-ons | Forum Rules
                        Like MODx? donate (and/or share your resources)
                        Like me? See my Amazon wishlist
                        MODx "Most Promising CMS" - so appropriate!