skip to Main Content

I have a registration form and a profile update form that I created a custom field with dropdown in. When the user selects the correct name and saves the data, it inserts the row ID instead of the name. IE:
Club 1
Club 2
Club 3
Club 4

If a user selects club 2, it inserts into meta "1" instead of "Club 2"
In the user update form I have it setup the way Ultimate members says in their documentation.

*DB Entry *enter image description here

The custom callback in functions.php per documentation

function ClubCallback() {
    global $wpdb; // WordPress database access abstraction layer

    // Table name assuming the table structure holds club names
    $table_name = $wpdb->prefix . 'wp_Club';

    // Query to select club names from the database
    $query = "SELECT ClubNames FROM $table_name";

    // Retrieving data from the database
    $results = $wpdb->get_results($query, ARRAY_A);

    $club = array();

    // Constructing an array of club names
    foreach ($results as $row) {
        $club[] = $row['Club'];
    }

    // Return the array of club names
    return array_unique($club);
}
add_shortcode('ClubCallback', 'clubCallback');

Form Image

This is what I used in their documentation
https://docs.ultimatemember.com/article/1884-custom-callback-dropdowns

    // Constructing an array of club names
    foreach ($results as $row) {
       //$club[] = $row['club'];
        $club[$row['club']][] = $row['club'];
    }

I tried the above but this does not work.

How can I get the result to show the club name instead of the value of the row?

2

Answers


  1. Chosen as BEST ANSWER

    I was able to figure out that in the array I had to contruct it so the key was first and the value was second. Here is my updated code.

       foreach ($results as $row) {
           $club[$row['club']] = $row['club'];
        }
    

  2. function ClubCallback() {
        global $wpdb; // WordPress database access abstraction layer
    
        // Table name assuming the table structure holds club names
        $table_name = $wpdb->prefix . 'wp_Club';
    
        // Query to select club names from the database
        $query = "SELECT ClubNames FROM $table_name";
    
        // Retrieving data from the database
        $results = $wpdb->get_results($query, ARRAY_A);
    
        $club = array();
    
        // Constructing an array of club names
        foreach ($results as $row) {
            $club[]['id'] = $row['meta_value'];
            $club[]['name'] = $row['meta_key'];
        }
    
        // Return the array of club names
        return array_unique($club);
    }
    add_shortcode('ClubCallback', 'clubCallback');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search