We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22996
    • 15 Posts
    I have created the following snippet to insert records into a custom table. As a general business rule, I only plan to assign a webuser to a single webgroup. So, my goal is to get the loginID of the current user and grab the webgroup for that user. Once I have the webgroup which is assigned to the webuser, I want to insert the webgroup value (integer) into the custom table as well.

    The snippet is working fine; however, the insert statement always inserts a "0" for the webgroup value. So, it does not appear that the getLoginID() function is returning a value within the snippet (even though I am logged in as a webuser). Is there an issue with the syntax or logic? Code is pasted below:

    <?php
    function addEvent2( &$fields )
    {
    // Bring needed resources into scope
    global $modx, $table_prefix;

    // Get the user id - this is used to find the webgroup
    $userID = $modx->getLoginUserID();

    $table = $modx->getFullTableName("web_groups");
    $wg = $modx->db->select("webgroup", $table, "webuser = $userID");

    // Get single values for the table
    $event_name = $fields[event_name]; // Add Event Name
    $event_description = $fields[event_description]; // Add Event Name
    $web_group_id = $wg;

    // Format sql statement
    $sql = "INSERT INTO ".$modx->getFullTableName("custom_events")." (event_name, event_description, web_group_id)
    VALUES(’".$event_name."’, ’".$event_description."’, ’".$wg."’);";
    $dbquery = $modx->db->query($sql);

    return true;
    }
    // Return empty string
    return ’’;
      • 22303 MODX Staff
      • 10,725 Posts
      $wg is a SQL result set, not the value of the column you are selecting. See $modx->db->getValue() if you want to retrieve a value directly. Otherwise you need to loop through the result set for each row and get the column value from the resulting array. See $modx->db->getRow()...
        • 22996
        • 15 Posts
        Aaaahhh. Thanks! Yes, that is just the result set smiley I added the "getrow" to obtain the single row of data. Below is the working code.

        <?php
        function addEvent2( &$fields )
        {
        // Bring needed resources into scope
        global $modx, $table_prefix;

        // Get the user id - this is used to find the webgroup
        $userID = $modx->getLoginUserID();

        $table = $modx->getFullTableName("web_groups");
        $wg = $modx->db->select("webgroup", $table, "webuser = $userID");

        // Get the single row from the SQL result set
        $row = $modx->db->getRow($wg);

        // Get single values for the table
        $event_name = $fields[event_name]; // Add Event Name
        $event_description = $fields[event_description]; // Add Event Name
        $web_group_id = $row[webgroup];

        // Format sql statement
        $sql = "INSERT INTO ".$modx->getFullTableName("custom_event")." (event_name, event_description, web_group_id)
        VALUES(’".$event_name."’, ’".$event_description."’, ’".$web_group_id."’);";
        $dbquery = $modx->db->query($sql);

        return true;
        }
        // Return empty string
        return ’’;
        ?>