I am an old Greek Pharmacist and my hobby is web development. So, I have this URL https://clinicaltrials.gov/api/query/study_fields?expr=lansoprazole&fields=NCTId%2CBriefTitle%2CCondition&min_rnk=1&max_rnk=10&fmt=json and I want to have all its JSON data in an html table. I tried with Fetch API, but I did not succeed :( How can this be done with JavaScript or PHP?
1 Answer
<?php
$jsondump = file_get_contents('https://clinicaltrials.gov/api/query/study_fields?expr=lansoprazole&fields=NCTId%2CBriefTitle%2CCondition&min_rnk=1&max_rnk=50&fmt=json');
// Decode JSON data to PHP associative array
$data = json_decode($jsondump, true);
if($data['StudyFieldsResponse']['MaxRank'] <= $data['StudyFieldsResponse']['NStudiesFound'])
{
$keynum=$data['StudyFieldsResponse']['MaxRank'];
}
else
{
$keynum=$data['StudyFieldsResponse']['NStudiesFound'];
}
echo ("NStudiesReturned: ") . $keynum;
echo "<hr><br>";
?>
<table border=1 width=800>
<tr><td>Rank</td><td>NCTId</td><td>BriefTitle</td><td>Condition</td></tr>
<?php
$x=0;
while ($x <= $keynum-1){
$rank = $data['StudyFieldsResponse']['StudyFields'][$x]['Rank'];
$nctid=$data['StudyFieldsResponse']['StudyFields'][$x]['NCTId'][0];
$title = $data['StudyFieldsResponse']['StudyFields'][$x]['BriefTitle'][0];
$condition_count=count($data['StudyFieldsResponse']['StudyFields'][$x]['Condition']);
//echo $condition_count;
echo "<tr><td> $rank</td><td>$nctid</td><td>$title</td><td><b>";
$ccx=0;
while ($ccx <= $condition_count-1){
$condition = $data['StudyFieldsResponse']['StudyFields'][$x]['Condition'][$ccx];
echo "<p>$condition</p>";
$ccx++;
}
echo "</b></td></tr>";
$x++;
}
?>
</table>
1 Comment
Community
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.