如何在 PHP 中获取 JSON 数据的分页?

How to get pagination for JSON data working in PHP?

通过我正在制作的网络服务,用户可以使用 'start' 和 'limit' 发送 URL 参数。 这样,他们可以选择每页显示多少个 JSON 个对象以及他们希望从哪一页开始。

这是我为此编写的 PHP 代码

$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));

分页计数与此完美结合。

我的问题是:如何让分页正常工作,从而使每页数据量有限,而多页数据?

编辑

这是 JSON 数据的片段

    {
      "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/"
      }
    ]
  }

试试这个

$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);

在您的浏览器中测试它。它在给定的简化 json

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