如何在PHP中使用JSON数据进行分页?

时间:2015-01-15 19:15:16

标签: php json pagination

通过我正在制作的网络服务,用户可以通过“开始”发送网址参数'和'限制'。 这样,他们可以选择每页显示多少个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/"
      }
    ]
  }

1 个答案:

答案 0 :(得分:2)

试试这个

$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
    }
  ]
}