• Resolved orodruim

    (@orodruim)


    Hello.
    I need to fill a select with the result of a SQL Query, and I don’t know how. Can you help me?
    Here is my code:

    function cf7_dynamic_select() {
    $choices = array(‘– Select a country –‘ => ”);
    $query =array($wpdb->get_results(‘SELECT * FROM wp_custom_countries’));

    $choices = array_merge($choices, $query);

    return $choices;
    }
    add_filter(‘my-filter’, ‘cf7_dynamic_select’, 10, 2);

    wp_custom_countries has de following structure:
    id iso nombre
    1 AF Afganistán
    2 AX Islas Gland
    3 AL Albania
    4 DE Alemania
    5 AD Andorra
    6 AO Angola
    7 AI Anguilla
    8 AQ Antártida
    9 AG Antigua y Barbuda
    10 AN Antillas Holandesas

    I would like to populate choices with the data

    ‘nombre1’ => ‘id1’, ‘nombre2’ => ‘id2’, …

    Could you help me to do this? I have no good skill in php.

    Thanks a lot.

    • This topic was modified 7 years, 3 months ago by orodruim.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter orodruim

    (@orodruim)

    If I use this code, the dropdown is filled with the number of countries, but no with their names. I mean, the dropdown shows 0,1,2,3,4,5… instead of Afganistán, Islas Gland, Albania…

    function cf7_dynamic_select() {
    global $wpdb;

    $countries=$wpdb->get_results(‘SELECT id, country FROM wp_custom_countries’);

    $choices = array(‘– Select one –‘ => ”);

    foreach ( $countries as $country ) {
    $choices[] = array( ‘text’ => $country->country, ‘value’ => $country->id);
    }

    return $choices;
    }
    add_filter(‘my-filter’, ‘cf7_dynamic_select’, 10, 2);

    Plugin Author John Huebner

    (@hube2)

    
    foreach ($countries as $country) {
      $choices[$country->country] = $country->id;
    }
    
    Thread Starter orodruim

    (@orodruim)

    Thanks, it works ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to make a SQL query’ is closed to new replies.