PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Cannot output results of WPDB query to drop down list

  1. Hi,

    I have the following function to display values from a custom database table in a DDL.

    function DDL_Sob($form){
        //only populating drop down for form id 27 - Referral
        global $wpdb;
    	if($form["id"] != 27)
           return $form;
    
    	$sql = "SELECT * FROM <code>bus_source</code> ORDER BY name";
    	$sobs = $wpdb->get_results($sql, ARRAY_N);
    
    	//Creating drop down item array.
        $items = array();
        $items[] = array("text" => "", "value" => "");
    
        foreach($sobs as $sob)
            {$items[] = array("value" => esc_attr($sob->name), "text" => esc_attr($sob->name));}
    
        //Adding items to field id 15. Replace 15 with your actual field id. You can get the field id by looking at the input name in the markup.
        foreach($form["fields"] as &$field)
            if($field["id"] == 15){
                $field["choices"] = $items;
            }
    
        return $form;
    }

    In mySQL the results are generated and in GF/WP the SELECT/OPTION statements are generated for each row but the values/text are empty as in:

    <select>
    <option value></option>
    <option value></option>
    <option value></option>
    </select>

    Any suggestions as to what I've gotten wrong?

    Posted 8 years ago on Thursday February 28, 2013 | Permalink
  2. Right after line 14, can you dump the $sob array and see what is contains? It's as if you have the correct number of items, but you're not referencing them correctly. If you can "print_r($sob)" between line 14 and 15, you will see why blank values are being assigned for the option text and value.

    Posted 8 years ago on Monday March 4, 2013 | Permalink