Search code examples
phpgravityforms

Unable to get values from array pulled from database


I am very new to php and gravity forms but have developed in other languages. I would like to pull a phone number from a table I created in Gravity Forms and display it in the Manager Phone # field dynamically. I have checked the box for allow field to be populated dynamically and added a parameter called MgrPhone. In my functions.php I added the filter gform_field_value_MgrPhone and the function set_mgr_phone. I am able to pull 995 records back but I can't get at the phone number values. I added a test array to make sure I understood and that works fine - so something in my database array setup. I have been working on this for way too long - any help appreciated!

Here is my code:

add_filter("gform_field_value_MgrPhone", "set_mgr_phone");
function set_mgr_phone($value){
    global $wpdb; 

    $sql = "SELECT PhoneNumber 
            FROM ADusers 
            WHERE phoneNumber IS NOT NULL 
        ";
    $results = $wpdb->get_results($sql);
    $myArray = array();

    foreach ($results as $result) {
        //$myArray[] = array("text" => "PhoneNumber" , "value" => $result->PhoneNumber );
        $myArray[] = array("value" => $result->PhoneNumber);
    }
    $a = array(1, 2, 3, 17);

    foreach ($a as $v) {
        echo "Current value of \$a: $v.\n";
    }
    foreach ($myArray as $v) {
        echo "Current value of \$myArray: $v.\n";
    }
    return "618-555-7777";
}

The results on screen look like this:

Current value of $a: 1. Current value of $a: 2. Current value of $a: 3. Current value of $a: 17. Current value of $myArray: Array. Current value of $myArray: Array. Current value of $myArray: Array. Current value of $myArray: Array. Current value of $myArray: Array. Current value of $myArray: Array. Current value of $myArray: Array. Current value of $myArray: Array. Current value of


Solution

  • You are adding a new Array to $myArray in the following line:

    $myArray[] = array("value" => $result->PhoneNumber);
    

    You don't need to do this (unless it's intended) so a solution could look like this.

    $myArray[] = $result->PhoneNumber;
    

    It would output the number as expected.