假設我透過curl, 對方透過json來我這直接給我一千筆數據
但這樣一次出現一千筆似乎不太對?如何透過php方式實現一頁20筆?有範本可參考?謝謝!
一點頭緒也沒有
function httpRequest ($api, $data_string)
{
$ch = curl_init($api);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
}
$x = array(
"session" => $_SESSION['session']
);
$x_string = json_encode($x);
$x = httpRequest($api, $x_string);
我是這樣取對方的數據的,只是要怎麼....做分頁?
理论上分页这种事情是接口处理的,一次一千条数据不太合理
最简单的方法你在请求接口时多带两个参数offset,count
offset为当前页码的数值,count为每页需要展示的数据量
后端接收到这两个参数后,计算出每次请求的数据的在数据库中的起始位置
eg: