Dynamically setting the selected value of a select list, after querying from the wpdb

I’m trying to dynamically set the selected option of a select list which is built inside a foreach loop. The values of the select list come from a custom table inside of the wordpress database.The function gets parsed a variable that contains the ticket priority that needs to be selected. My current code base is:

function create_select($ticket_priority){
global $wpdb;

$query = "SELECT * FROM st_support_priorities";
$allrecs = $wpdb->get_results($query);

$buffer = '<select name="ticket_priority">';
foreach ($allrecs as $rec) {
    $buffer .= '<option value="'.$rec->ticket_priority.'" ';
        if($ticket_priority == "$rec->ticket_priority."){
            $buffer .= 'selected';
        }
    $buffer .= '>'.$rec->ticket_priority.'</option>';
}
$buffer .= '</select>';

echo $buffer;
}

There are a lot of little intricate parts to the building of each select element, so I’m sure I’m missing something, just can’t see what it is. I get no errors when running this and the select list is being built properly but the priority being parsed in is not being selected.

Related posts

Leave a Reply

1 comment