I am trying to figure out how I count the difference, in PHP arrays results.
I got this:
3 different API Endpoints where I collect data from. I merge that data into arrays, with 15 results(5 from each API), in each array. I use a pagination to loop through the arrays. But if one of the API's return 0 results, my arrays become 10 long, instead of 15.
My Question:
Is there a way to count the API results from each endpoint, figure out if one/more endpoints dont respond with 5 results(15 in total for all 3 endpoints), and add it to the last endpoint/split the two others, and keep the results at 15, until there a no more results from all 3 endpoints?
API Control:
The endpoints are controlled with parameters.
api.domain.com/sofas&per_page=5
api.domain.com/chairs&per_page=5
api.domain.com/tabels&per_page=5
I have no clue how to do this, so all help are taken with kisses and hugs.
Edit:
The pagination is not the problem. I will always, no matter what, max get 15 results back, in total. The problem is that it will go from showing 15 results to 13 results per page. In the case where both sofas and chairs has run out, will it only show 5 per page. Because the ?per_page parameter dont know that two of the endpoints is empty and will continue to return 5 results.
------
$rowCount = 0;
foreach($api_results as $result){
// If Sofa
if($result['sofa']){
$rowCount++;
echo 'Sofa';
// If Chair
}else if($result['chair']){
$rowCount++;
echo 'Chair';
// If Table
}else if($result['table']){
$rowCount++;
echo 'Tables';
}
}
echo '</ol>';
// How many items to list per page
$limit = $rowCount;
// How many pages will there be
$pages = ceil($total / $limit);
// Calculate the offset for the query
$offset = ($page - 1) * $limit;
// Some information to display to the user
$start = $offset + 1;
$end = min(($offset + $limit), $total);
//
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
PHP
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license. https://www.php.net/
Welcome to programmierfrage.com
programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.
This site uses cookies. We use them to improve the performance of our website and your interaction with it. Confirm your consent by clicking OK