0

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
  • Please provide enough code so others can better understand or reproduce the problem. Commented Jan 23, 2022 at 7:03

1 Answer 1

1
<?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>
Sign up to request clarification or add additional context in comments.

1 Comment

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.

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.