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

    I needed to change the Personalize snippet. The personalize snippet currently uses a get method (getLoginUserName) to get the username and returns it appended to a custom message if desired.

    I wanted to get the fullname field from the database...so what I thought to do was:
    $o = ’ ’;
    $person = $modx->getLoginUserID(); #This returns the userid which is the foreign key from the web_user_attributes
    #Then make a call to the database:

    $sql = "SELECT fullname FROM ".$modx->getFullTableName("web_user_attributes")." WHERE internalKey=’$person’";
    $o = ($sql)? "$message$sql" : ’’; #set it into my variable o
    return $o; #return it

    Unfortunately, this doesn’t make a query...it simply spits out "SELECT fullname FROM...etc.

    If anyone can help in how to make sql commands in a snippet, I would appreciate it smiley

    Thanks,

    Josh
      • 2472
      • 151 Posts
      I figured this out by reading some existing snippets... lots of them access to modx database.
        A thing of beauty is a joy forever ( John Keats)
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        You have to make the query with the $sql string you specified:
        $result = $modx->db->query($sql);

        Or you can use the class select function:
        $results = $modx->db->select("fullname", $modx->getFullTableName("web_user_attributes"), "internalKey='".$person."'");

        and in either case, then get the value from the result:
        $fullname = $modx->db->getValue($result);


        Take a look through the manager/includes/extenders/dbapi.mysql.class.inc.php file for all the db API goodies.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 14796
          • 59 Posts
          Once again thank you for the help Susan smiley it worked perfectly!

          You’ve really been helping the learning process!

          Aside from the API Quickreference section of Documentation of methods and the ones from the dbapi, are there anyother ones I should be aware of?

          Thanks,

          Josh

            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Well, let’s just say a major portion of my reading (I don’t have a television so I do a lot of reading) is taken up with the source code of the various MODx files. Mainly, the parser itself (document.parser.class.inc.php). Many of the functions aren’t suitable for external use, but a lot of them can be very useful.

            With the DB API, some fun queries can be worked out. For example, to get a list of usernames belonging to a given web group:

            $results = $modx->db->select("tbl1.username", "$table1 AS tbl1, $table2 AS tbl2, $table3 AS tbl3", "tbl2.name='$webgroup' AND tbl3.webgroup=tbl2.id AND tbl1.id = tbl3.webuser", 'tbl1.username ASC');
            


            The core snippets, such as Wayfinder and Ditto, make for good study materiel. Right now I’m looking at how best to add pagination to the FileDownload snippet, and I’ll be using other snippets as reference materiels.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 14796
              • 59 Posts
              Great!

              Thank you for the helpful tidbit smiley