Call Api, get json, populate select

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?

What is the overall goal for the user during one browser session? To see a select/option menu with x choices (which could be several hundred values) and select a single or multiple project(s), or should the user be able to filter and narrow down the choices, then make a selection and do you want to continue to re-read all the values from the api for each web page request the user causes, or would just operating on the initial downloaded copy of the api data, saved in a session variable, during the single browser session be okay?

You would just keep appending the data values you are interested in to an array variable. If you want that data to persist for the duration of a single browser session, you would use a session array variable.

Once you have all the data in an array variable, you can sort it, filter it, loop over it to produce the select/option menu.

1 Like

It’s like a portfolio, you view your projects in a real simple model viewer with some simple data.
Anyone can make their app structure with website tools provided for this, and you set what projects you want to show public.

The select has the name the project you share, and I guess probably going to need to make a call each time you click on a project to view it.
They give tutorials and stuff to make in “C” I think it was, the thing is I know nothing about “C” or any other language and I’m already on my road of learning html5,css,php, javascript, and I decided to make my own using php.

I’ll check this out, thanks.

Edit: Ok managed to do it just like you said appending data to an array variable, also the sorting was simple now. Thanks.
For some reason I don’t see the solution button to click, but its solved.

Sponsor our Newsletter | Privacy Policy | Terms of Service