plugins - Populate select option with JSON file

admin2025-01-07  4

I am having a problem with populating a select dropdown from a JSON file. I am modifying an existing plugin, and I want to include countries that we want to see to.

The JSON FILE

{
 "AX": {
    "name": "Åland Islands",
    "lpa": "18",
    "affirm_non_muslim": false
 },
 "AF": {
    "name": "Afghanistan",
    "lpa": "0",
    "affirm_non_muslim": false
 },
 "AL": {
    "name": "Albania",
    "lpa": "18",
    "affirm_non_muslim": false
 },
 "DZ": {
    "name": "Algeria",
    "lpa": "18",
    "affirm_non_muslim": false
 }
}

MY FUNCTION FILE

function av_get_json_data(){

    $json_url = require( plugin_dir_path( __FILE__ ) . '../includes/data/country_age.json' );
    $args   = array('timeout' => 120);
    $json_feed = wp_remote_get( $json_url, $args );

    $json_string_in_array = array('$json_feed');
    $json_array = json_decode($json_string_in_array[0]);

    //$actual_feed  = json_decode($json_array);
    return $json_array;
}

Here is the output form

$form .= '</select> <select name="av_verify_l" id="av_verify_l">';

        foreach (av_get_json_data() as $data) :
            $form .= '<option value="" >' . $data . '</option>';
        endforeach;
        $form .='<select>';

FRONT-END OUTPUT

QUESTION

How can I output the JSON file countries in the dropdown

I am having a problem with populating a select dropdown from a JSON file. I am modifying an existing plugin, and I want to include countries that we want to see to.

The JSON FILE

{
 "AX": {
    "name": "&Aring;land Islands",
    "lpa": "18",
    "affirm_non_muslim": false
 },
 "AF": {
    "name": "Afghanistan",
    "lpa": "0",
    "affirm_non_muslim": false
 },
 "AL": {
    "name": "Albania",
    "lpa": "18",
    "affirm_non_muslim": false
 },
 "DZ": {
    "name": "Algeria",
    "lpa": "18",
    "affirm_non_muslim": false
 }
}

MY FUNCTION FILE

function av_get_json_data(){

    $json_url = require( plugin_dir_path( __FILE__ ) . '../includes/data/country_age.json' );
    $args   = array('timeout' => 120);
    $json_feed = wp_remote_get( $json_url, $args );

    $json_string_in_array = array('$json_feed');
    $json_array = json_decode($json_string_in_array[0]);

    //$actual_feed  = json_decode($json_array);
    return $json_array;
}

Here is the output form

$form .= '</select> <select name="av_verify_l" id="av_verify_l">';

        foreach (av_get_json_data() as $data) :
            $form .= '<option value="" >' . $data . '</option>';
        endforeach;
        $form .='<select>';

FRONT-END OUTPUT

QUESTION

How can I output the JSON file countries in the dropdown

Share Improve this question asked Jan 20, 2017 at 17:28 AneleAnele 14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Edit output form with $form .= '<option value="" >' . $data['name'] . '</option>';

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736260480a668.html

最新回复(0)