hridaynehu hridaynehu - 6 months ago 15
JSON Question

Print JSON string in dropdown menu

I am getting a problem in generating values of a JSON string into a dropdown menu.I have a JSON API

http://service.proventustechnologies.com/api/json?username=demo&token=4ce26206-025d-11dc-8314-0800200c9a66&method=getattributevalues&dimensionId=10


When you click it you will see many values like this.

{
"response": {
"id": 10,
"values": [
{
"id": 258,
"value": "Cameras"
},
{
"id": 2581,
"value": "GPS Navigation"
},
{
"id": 259,
"value": "MP3 Players"
},
{
"id": 260,
"value": "Cell Phone/PDA"
},
{
"id": 6377,
"value": "Digital Photo Frame"
},
{
"id": 110,
"value": "Motherboard"
},
{
"id": 108,
"value": "Desktop/Server"
},
{
"id": 109,
"value": "Notebook"
},
{
"id": 10738,
"value": "E-Book Reader"
}
],
"name": "System Type"
},
"status": "ok"


}

I want to print all the values such as Cameras,GPS Navigation in a dropdown menu.Thank you for any kind of help.

Answer

I can't really get a feel for what your level of knowledge is, but you should load your data in a html select element for example like this:

<?php
    echo '<select>';

    $jsonData = file_get_contents($url); 
    $jsonDataObject = json_decode($jsonData);

    foreach($jsonDataObject->response->values as $option){
        echo '<option value=' . $option->id . '>' . $option->value . '</option>';
    }

    echo '</select>';
?>
Comments