I am new to php and trying to make dropdown list work with wordpress user table
I am trying to set user id as a value and want to display user_login but not able to get properly. It is rendering two time with wrong id. Here is my code
global $wpdb;
$userids = $wpdb->get_col("SELECT ID FROM $wpdb->users");
$user_logins = $wpdb->get_col("SELECT user_login FROM $wpdb->users");
echo '<select>';
foreach($userids as $userid){
foreach($user_logins as $user_login)
echo '<option value="'.$userid.'" selected="'.$user_login.'">'.$user_login.'</option>';
}
echo '</select>';
Thanks a lot
If you need to use the two-queries that you’re currently using, you can iterate with a counter instead of
foreach:You can combine the two queries into a single query using WordPress’s
get_results()method:It’s also worth pointing out, the
option‘sselectedattribute is being improperly used. It should only be set on a single option, and is generally set with a value of “selected”, such asselected="selected". Refer to the W3C documentation for more info regarding that though.