Pagination

API Pagination for returned bulk data.

Pagination explained

Sometimes the data returned is just to long to display at once, such as product list, or branch list...etc. Displaying all this output data in one page will cause the app to load slow for the end users, so the pagination is the best option to control the output data to our needs.

Page parameter

Certain routes, such as index listing may return an array of results.

By default, the API will return the results in batch. The page parameter may be used to increase the number of results per request.

To get the next batch of results, call the same route again with a page request parameter corresponding to the next_page and total property received in the last call on the pagination part of the response.

The returned JSON data will work with the page specified or no, but it's a good practice to use it, to make your app load faster and give your users best experience possible.

Example

Notice the URLOPT_POSTFIELDS => "{\"page\":5}",

You could also use null to start fro page 1 and give next page as 2

 <?php
 
 $curl = curl_init();

curl_setopt_array($curl, [
 CURLOPT_URL => "https://sandbox.mnasati.com/v1/orders",
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_ENCODING => "",
 CURLOPT_MAXREDIRS => 10,
 CURLOPT_TIMEOUT => 30,
 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
 CURLOPT_CUSTOMREQUEST => "POST",
 CURLOPT_POSTFIELDS => "{\"page\":5}",
 CURLOPT_HTTPHEADER => [
   "Authorization-Jwt: Jwt eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ2ZW5kb3JfaWQiOiI0MyIsInVzZXJfaWQiOiI0MCIsInVzZXJfdHlwZSI6InZlbmRvciIsImVtYWlsIjoiemF6YUB1aWd0Yy5jb20iLCJpYXQiOjE2MjE0OTE0ODYsImV4cCI6MTYyNjY3NTQ4Nn0.Pa870uzsaRvFayG8S7oBlS_y66vCN0r4L_ab5A8SpqQ",
   "Content-Type: application/json"
 ],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
 echo "cURL Error #:" . $err;
} else {
 echo $response;
}

Here is the response of the page parameter for pagination.

JSON data returned
{
   "status":true,
   "total":6341,
   "next_page":6,
   "orders":[
      {
         "sale_id":"7049",
         "sale_code":"KNCZ-7049",
         "buyer_name":"dania",
         "buyer_email":"",
         "buyer_phone":"999999999",
         "buyer_phone_code":"974",
         "fund_number":"",
         "shipping":"30.000",
         "payment_type_id":"1",
         .....................

And if you were to enter an invalid (non existence page number) in your call, you would get the following response:

{
   "status":true,
   "total":6341,
   "next_page":null,
   "orders":[
      
   ]
}

Last updated