Hello, I’m working on a list for some projects. Using and Api provided by a server with a limited data per page of 200.
So I’ve done this code and then I’ve added do while loop to take all the projects out and jump from page to page.
$pageN = 0;
do {
$url = "https://$api/$parameters?page[number]=$pageN&page[limit]=200";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Authorization: Bearer $bearerN",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
// debug
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// results
$resp = curl_exec($curl);
curl_close($curl);
//var_dump($resp);
$projects = json_decode($resp,true);
echo "Page Number = $pageN<br>";
echo 'Total Projects = '.$totalProjects = count($projects['data']).'<br>';
for ($x = 0; $x<count($projects['data']); $x++) {
echo $x.' - '.$projects['data'][$x]['attributes']['name'].'<br>';
}
$pageN++;
echo '<br>';
} while ($x == 200);
so basically its listing all in the same page something like this
Page Number = 0
Total Projects = 200
0 - Project M
1 - Project B
…
…
199 - Project A
it has a limit of 200 total data determinated by the server, so I loop and if theres a Total of 200 it will jump to another page and do the same, etc.
I wanted to populate a select so I’ve added
<?php echo "$totalProjects Projects"; ?>
<select>
<?php foreach($projects['data'] as $data): ?>
<option value="<?php echo $data['attributes']['name'] ?>"><?php echo $data['attributes']['name'] ?></option>
<?php endforeach ?>
</select>
if you have less than 200 projects it works fine, if you have more it will only show you the total projects of the last page of the loop. And it will populate the select with only the last page of the loop also.
What kind of temporary storing method can I use or how should I aproach this?
I noticed there was going to be a trouble when I echo the list the first time, I had all in the same page with the header and the marked page Numbers marking the jump, the total projects per page different projects but the count of the projects where the same.
Page Number = 0
Total Projects = 200
0 - Project M
1 - Project B
…
…
199 - Project A
Page Number = 1
Total Projects = 78
0 - Project C66
1 - Project B9
…
…
78 - Project A45
Also I’ve been tryng to sort the json given by the call, but with no success, the order is the same as If you saw it from their site. This dosen’t bother me much since I’ve added a search filter to my select but was curious on how to do aproch it since I would have to have all the list completed then sorting it I think?