user3182261
user3182261

Reputation: 271

How to get pagination for JSON data working in PHP?

With the webservice I'm making, users are able to send URL parameters with 'start' and 'limit'. That way, they can choose how many JSON objects are shown per page and at what page they want to start.

This is the PHP code I've made for this

$start = (int)$_GET['start'];
        $limit = (int)$_GET['limit'];

        switch ($start) {
            case 1:
                $previous = 1;
                break;
            default:
                $previous = $start - 1;
        }

        $next = $start + 1;

        $file = file_get_contents("data.json");
        $data = json_decode($file);

        $total_items = count($data->items);
        $total_pages = round($total_items / $limit);

        $first = 1;

        $data->pagination = array(
            'currentPage' => $start,
            'currentItems' => $limit,
            'totalPages' => $total_pages,
            'totalItems' => $total_items,
            'links' => [
                array(
                    "rel" => "first",
                    "page" => $first,
                    "href" => "http://somelink/restful/?start=$first&limit=$limit"
                ),
                array(
                    "rel" => "last",
                    "page" => $total_pages,
                    "href" => "http://somelink/0879644/restful/?start=$total_pages&limit=$limit"
                ),
                array(
                    "rel" => "previous",
                    "page" => $previous,
                    "href" => "http://somelink/0879644/restful/?start=$previous&limit=$limit"
                ),
                array(
                    "rel" => "next",
                    "page" => $next,
                    "href" => "http://somelink/0879644/restful/?start=$next&limit=$limit"
                )
            ]
        );

        file_put_contents('data.json', json_encode($data));

Pagination counts work perfectly with this.

My question is: how do I get the pagination to work properly, so with a limited amount of data per page and multiple pages with data?

EDIT

This is a fragment of the JSON data

    {
      "items": [
        {
          "id": 1,
          "title": "Lorem",
          "artist": "Ipsum",
          "genre": "Pop",
          "week": "4",
          "highest_rating": "3",
          "year": "2014",
          "youtube": "www.youtube.com",
          "links": [
            {
              "rel": "self",
              "href": "somelink/0879644/restful/music/1"
            },
            {
              "rel": "collection",
              "href": "somelink/0879644/restful/music"
            }
          ]
        }
 ]
    }
  ],
"pagination": {
    "currentPage": 1,
    "currentItems": 1,
    "totalPages": 1,
    "totalItems": 8,
    "links": [
      {
        "rel": "first",
        "page": 1,
        "href": "somelink/0879644/restful/"
      },
      {
        "rel": "last",
        "page": 1,
        "href": "somelink/0879644/restful/"
      },
      {
        "rel": "previous",
        "page": 1,
        "href": "somelink/0879644/restful/"
      },
      {
        "rel": "next",
        "page": 2,
        "href": "somelink/0879644/restful/"
      }
    ]
  }

Upvotes: 1

Views: 4354

Answers (1)

Jakub Filipczyk
Jakub Filipczyk

Reputation: 1141

Try this

$start = (int)$_GET['start'];
$limit = (int)$_GET['limit'];

switch ($start) {
    case 1:
        $previous = 1;
        break;
    default:
        $previous = $start - 1;
}

$next = $start + 1;

$file = file_get_contents("data.json");
$data = json_decode($file);

$total_items = count($data->items);
$total_pages = ceil($total_items / $limit);

$data->items = array_slice($data->items, ($start-1)*$limit, $limit);

$first = 1;

$data->pagination = array(
    'currentPage' => $start,
    'currentItems' => $limit,
    'totalPages' => $total_pages,
    'totalItems' => $total_items,
    'links' => [
        array(
            "rel" => "first",
            "page" => $first,
            "href" => "http://somelink/restful/?start=$first&limit=$limit"
        ),
        array(
            "rel" => "last",
            "page" => $total_pages,
            "href" => "http://somelink/0879644/restful/?start=$total_pages&limit=$limit"
        ),
        array(
            "rel" => "previous",
            "page" => $previous,
            "href" => "http://somelink/0879644/restful/?start=$previous&limit=$limit"
        ),
        array(
            "rel" => "next",
            "page" => $next,
            "href" => "http://somelink/0879644/restful/?start=$next&limit=$limit"
        )
    ]
);

echo json_encode($data);

Test it in your browser. It paginates over given simplified json

{
  "items": [
    {
      "id": 1
    },
    {
      "id": 2
    },
    {
      "id": 3
    },
    {
      "id": 4
    }
  ]
}

Upvotes: 3

Related Questions