0

I got a piece of PHP code from a website API to retrieve the data and i would like the data array to be arranged in a select option drop down menu. How can i do that? Below is the code provided by the web:

<?php
$data = array(

    'token' => '4B0D42AB9D061C0FADD724D2E908349D',
    'content' => 'report',
    'format' => 'json',
    'report_id' => '71',
    'rawOrLabel' => 'label',
    'rawOrLabelHeaders' => 'label',
    'exportCheckboxLabel' => 'false',
    'returnFormat' => 'json');
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://redcap-virtualbox/redcap/api/');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_VERBOSE, 0);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($ch, CURLOPT_AUTOREFERER, true);

curl_setopt($ch, CURLOPT_MAXREDIRS, 10);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data, '', '&'));

$output = curl_exec($ch);

print $output;

curl_close($ch);

?>

The data output generated as shown below:

[{"project_code":"17001"},{"project_code":"17002"},{"project_code":"17003"},{"project_code":"17004-CG"},{"project_code":"17005"},{"project_code":"16001"},{"project_code":"16002"},{"project_code":"16003"},{"project_code":"16004"},{"project_code":"16005"},{"project_code":"16006"},{"project_code":"16007"},{"project_code":"16008"},{"project_code":"16009"},{"project_code":"16010"},{"project_code":"16011"},{"project_code":"16012"},{"project_code":"16013"},{"project_code":"16014"},{"project_code":"16015"},{"project_code":"16016"},{"project_code":"17006"},{"project_code":"17008"},{"project_code":"17007"},{"project_code":"16017"},{"project_code":"16018"},{"project_code":"17009"},{"project_code":"17010"},{"project_code":"17011"},{"project_code":"17012"},{"project_code":"17013"},{"project_code":"17014"},{"project_code":"17015"},{"project_code":"17016"},{"project_code":"17017"},{"project_code":"17018"},{"project_code":"17019"},{"project_code":"17020"},{"project_code":"16019"},{"project_code":"17021"},{"project_code":"17022"},{"project_code":"16021"},{"project_code":"17023"},{"project_code":"16023"},{"project_code":"16024"},{"project_code":"16025"},{"project_code":"16026"},{"project_code":"16027"},{"project_code":"16028"},{"project_code":"16029"},{"project_code":"16030"},{"project_code":"16031"},{"project_code":"17024"},{"project_code":"16032"},{"project_code":"17025"},{"project_code":"17026-CG"},{"project_code":"17027"},{"project_code":"17028"},{"project_code":"17029"},{"project_code":"17030"},{"project_code":"17031"},{"project_code":"16033"},{"project_code":"16034"},{"project_code":"17032"},{"project_code":"17033"},{"project_code":"17034"},{"project_code":"17035"},{"project_code":"17036"},{"project_code":"17037"},{"project_code":"17038"},{"project_code":"17039"},{"project_code":"17040-CG"},{"project_code":"17041"},{"project_code":"17042"},{"project_code":"17044-CG"},{"project_code":"17045-CG"},{"project_code":"17043"},{"project_code":"17046"},{"project_code":"17047"},{"project_code":"17048"},{"project_code":"17049"},{"project_code":"17050"},{"project_code":"17051"},{"project_code":"17052"},{"project_code":"17053"},{"project_code":"17054"},{"project_code":"17055"},{"project_code":"17056"},{"project_code":"17057"},{"project_code":"17059-CG"},{"project_code":"17060"},{"project_code":"17061"},{"project_code":"17062"},{"project_code":"17063"},{"project_code":"17064"},{"project_code":"17065"},{"project_code":"17066"},{"project_code":"17067"},{"project_code":"17068"},{"project_code":"17069"},{"project_code":"17070"},{"project_code":"17071"},{"project_code":"17072"},{"project_code":"17073"},{"project_code":"17074"},{"project_code":"17075"},{"project_code":"17076"},{"project_code":"17077"},{"project_code":"17078"},{"project_code":"17079"},{"project_code":"17080"},{"project_code":"17081"},{"project_code":"17082"},{"project_code":"17083"},{"project_code":"17084"},{"project_code":"17085"},{"project_code":"17086"},{"project_code":"17087"},{"project_code":"17088"},{"project_code":"17089-CG"},{"project_code":"17090"},{"project_code":"17091"},{"project_code":"17092"},{"project_code":"17093"},{"project_code":"17094-CG"},{"project_code":"17095"},{"project_code":"17096"},{"project_code":"17097"},{"project_code":"17098"},{"project_code":"17099"},{"project_code":"17100"},{"project_code":"17101"},{"project_code":"17102"},{"project_code":"17103"},{"project_code":"17104"},{"project_code":"17105"},{"project_code":"17106"},{"project_code":"17107"},{"project_code":"18001"},{"project_code":"18002"},{"project_code":"16035"},{"project_code":"18003"},{"project_code":"18004-CG"},{"project_code":"18005"},{"project_code":"18006"},{"project_code":"18007"},{"project_code":"18008"},{"project_code":"18009"},{"project_code":"18010"},{"project_code":"18011"},{"project_code":"18012"},{"project_code":"18013"},{"project_code":"18014"},{"project_code":"18015"},{"project_code":"18016"},{"project_code":"18017"},{"project_code":"18018"},{"project_code":"18019"},{"project_code":"18020"},{"project_code":"18022"},{"project_code":"18023"},{"project_code":"18024"},{"project_code":"18025"},{"project_code":"18026"},{"project_code":"18027"},{"project_code":"18028"},{"project_code":"18029"},{"project_code":"18030"},{"project_code":"18031-CG"},{"project_code":"18032"},{"project_code":"18033"},{"project_code":"18034-CG"},{"project_code":"18035"},{"project_code":"18036"},{"project_code":"18037"},{"project_code":"18038"},{"project_code":"18039"},{"project_code":"18040"},{"project_code":"18041"}]

Basically I just need the Project code numbers to be stored in SELECT option menu. Can anyone help please? Your help is much appreciated.

2
  • this is json , encode it & output shows in select tag. Commented Jun 7, 2018 at 1:55
  • @MohitKumar Hi do you mind showing me how? I'm not very familiar with JSON. Thanks! Commented Jun 7, 2018 at 1:57

2 Answers 2

0

Get rid of the print statement. That just spits out the JSON.

You need to decode the JSON, and loop through it inside a select element.

Replace the print statement with:

$json = json_decode( $output );

echo '<select name="my-select">';
foreach( $json as $item ){
    echo "<option value='{$item->project_code}'>Code: {$item->project_code}</option>";
}
echo '</select>';

What this does is output a basic select and then loop through the decided API response, echoing an option with value for each one.

Update:

Since I'm not sure if you know what all the keys are, we can push them into an associative array:

$json = json_decode( $output );

//Create an Empty Array, we'll add selects and options to this
$selects = [];

foreach( $json as $item ){
    foreach( $item as $key => $val ){
        $selects[$key][] = $val;
    }
}

//Now we have an associative array of keys (project_code, dept, etc.)
//and those have arrays associate with the appropriate key

foreach( $selects as $name => $options ){
    echo "<select name='$name'>";
        echo "<option value=''>Choose a $name</option>";
        foreach( $options as $option ){
            echo "<option value='$option'>$option</option>";
        }
    echo '</select>';
}
Sign up to request clarification or add additional context in comments.

4 Comments

btw what if my json data has more than one field (e.g. project codes, department, project title etc.) how do i generate multiple select option for these fields using foreach? Can help?
I'd need to see what the JSON looks like exactly
It looks like this: [{"project_code":"18041","dept":"Clinical Research and Innovation Office (CRIO)","group":"Medical"},{"project_code":"18040","dept":"Orthopaedic Surgery (ORTHO)","group":"Medical"},{"project_code":"18039","dept":"Orthopaedic Surgery (ORTHO)","group":"Medical"},{"project_code":"18038","dept":"Physiotherapy (PT)","group":"Allied Health Services"},{"project_code":"18037","dept":"Otorhinolaryngology (ENT)","group":"Medical"}]
Apologies, was away from my computer. That should be easy enough, I've updated the code to dynamically generate the the dropdowns for you by creating an associative array and looping through that. Let me know if you have any questions, I added some comments to help - and it should be relatively self explanatory
0

Use below code:

$json = json_decode($output, true);

$select = '';
$select .= '<select name="NAME">';
foreach($json as $val)
{
    $select .= '<option value="'.$val['project_code'].'">'.$val['project_code'].'</option>';
}
$select .= '</select>';
echo $select;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.