Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8332499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:44:32+00:00 2026-06-09T02:44:32+00:00

I am new to php and trying to make dropdown list work with wordpress

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:44:33+00:00Added an answer on June 9, 2026 at 2:44 am

    If you need to use the two-queries that you’re currently using, you can iterate with a counter instead of foreach:

    $count = count($userids);
    for ($i = 0; $i < $count; $i++) {
        echo '<option value="' . $userids[$i] . '" selected="' . $user_logins[$i] . '">' . $user_logins[$i] . '</option>';
    }
    

    You can combine the two queries into a single query using WordPress’s get_results() method:

    $users = $wpdb->get_results("SELECT ID, user_login FROM $wpdb->users");
    echo '<select>';
    foreach ($users as $user) {
        echo '<option value="' . $user->ID . '" selected="' . $user->user_login . '">' . $user->user_login . '</option>';
    }
    echo '</select>';
    

    It’s also worth pointing out, the option‘s selected attribute is being improperly used. It should only be set on a single option, and is generally set with a value of “selected”, such as selected="selected". Refer to the W3C documentation for more info regarding that though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Wordpress (and PHP). I am trying to make WP MVC's
I am pretty new to PHP and I am trying to make an inventory
i'm new related to creating php class's and objects... I'm trying to make a
I am new to php. Trying to send confirmation about user upload. I am
I'm brand new to php and I'm trying to work with some includes in
I'm trying to make a message board with php, but when a new message
I am trying to make a check in PHP if a user changes their
I am new to PHP and mysql and i am trying to make API's
I'm trying to make custom pages using the new vBulletin 4. My PHP file
After 4 years of absence in PHP programming I'm trying to make some new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.