我的代码:
<?php
$array = json_decode('{ "friends": { "data": [ { "id": "1000002470615", "gender": "female" }, { "id": "1000005198891", "gender": "female" }, { "id": "1000007859390", "gender": "female" }, { "id": "1000008308250", "gender": "female" }, { "id": "1000009416380", "gender": "male" }, { "id": "1000010894609", "gender": "female" }, { "id": "1000010991284", "gender": "male" }, { "id": "1000011095914", "gender": "female" }, { "id": "1000014648465", "gender": "female" }, { "id": "1000017041889", "gender": "female" }, { "id": "1000017519651", "gender": "female" }, { "id": "1000019029610", "gender": "female" }, { "id": "1000019497511", "gender": "female" }, { "id": "1000019681751", "gender": "female" }, { "id": "1000020582845", "gender": "male" }, { "id": "1000021389766", "gender": "male" }, { "id": "1000022247272", "gender": "female" }, { "id": "1000025425217", "gender": "female" }, { "id": "1000026359294", "gender": "female" }, { "id": "1000031258301", "gender": "male" }, { "id": "1000031499108", "gender": "female" }, { "id": "1000032349067", "gender": "male" }, { "id": "1000032428345", "gender": "female" }, { "id": "1000033101313", "gender": "male" }, { "id": "1000033288824", "gender": "female" }, { "id": "1000033525527", "gender": "female" }, { "id": "1000034062687", "gender": "male" }, { "id": "1000035232214", "gender": "female" }, { "id": "1000035804034", "gender": "female" }, { "id": "1000040066742", "gender": "male" }, { "id": "1000041457943", "gender": "female" }, { "id": "1000042302703", "gender": "female" }, { "id": "1000043758095", "gender": "female" }, { "id": "1000045378342", "gender": "male" }, { "id": "1000048531666", "gender": "female" }, { "id": "1000049731704", "gender": "female" }, { "id": "1000050719295", "gender": "female" }, { "id": "1000050736082", "gender": "male" }, { "id": "1000051733891", "gender": "female" }, { "id": "1000052976810", "gender": "male" }, { "id": "1000053432719", "gender": "male" }, { "id": "1000054595524", "gender": "male" }, { "id": "1000055567339", "gender": "female" }, { "id": "1000056180174", "gender": "female" }, { "id": "1000056737207", "gender": "male" }, { "id": "1000058322123", "gender": "male" }, { "id": "1000058592478", "gender": "female" }, { "id": "1000059646724", "gender": "male" }, { "id": "1000062848532", "gender": "female" }, { "id": "1000062994936", "gender": "female" }, { "id": "1000066441440", "gender": "male" } ] } }', true);
foreach($array['friends']['data'] as $details) {
echo $details['id'], '<br />';
}
?>
结果:
1000002470615
1000005198891
1000007859390
1000008308250
1000009416380
1000010894609
1000010991284
1000011095914
1000014648465
1000017041889
1000017519651
1000019029610
1000019497511
...
如何定义我的分页,如下例所示?
1000002470615
1000005198891
1000007859390
1000008308250
1000009416380
<--Previous Next-->
你应该用什么才能做到这一点。 对不起我的英文表达。
答案 0 :(得分:0)
只需使用会在某些迭代中断的递增器,因此下一页将从结果中迭代,大于递增器达到的数量。我将提供一个包含更简单数组的解决方案,你也可以在你的情况下使用它,也许你需要一个额外的内部变量用于数字键。
<?php
session_start();
$array = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
if (!isset($_GET['next'])) {
$_SESSION['start'] = 0; // if we are not using pages, we start from the beginning of the array
}
else {
$_SESSION['start']+=5; //if we have clicked Next page, we start from the results reached (that's how many results you want to reach)
}
$i = 0; // start from the beginning
foreach ($array as $k => $v) {
if ($i > $_SESSION['start']+4) { //if we have reached the starting point + how many results
break;
}
if ($k >= $_SESSION['start']) { //if the numeric key has reached the starting point (i.e. on second page it should not print results, before the foreach reaches the fifth key.
echo $v . "<br />";
}
$i++;
}
?>
<a href="test2.php?next">Next</a>
此处输出:
1
2
3
4
5
Next
点击下一步:
6
7
8
9
10
Next
如果到达结束指针,您将需要进行某些检查,并且需要使用“上一步”按钮来减少您想要达到的结果。
可能是这样的
<?php
session_start();
$array = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
if (!isset($_GET['next']) && !isset($_GET['previous'])) {
$_SESSION['start'] = 0;
}
if (isset($_GET['next'])) {
$_SESSION['start']+=5;
}
if (isset($_GET['previous'])) {
$_SESSION['start']-=5;
}
$i = 0;
$endReached = false;
$hasBegun = false;
foreach ($array as $k => $v) {
if ($i > $_SESSION['start']+4) {
break;
}
if ($k >= $_SESSION['start']) {
echo $v . "<br />";
if($k == $array[0]) {
$hasBegun = true;
}
if($v == end($array)) {
$endReached = true;
}
}
$i++;
}
?>
<?php if(!$endReached): ?>
<a href="test2.php?next">Next</a>
<?php endif; ?>
<?php if(!$hasBegun): ?>
<a href="test2.php?previous"> Previous </a>
<?php endif; ?>
答案 1 :(得分:0)
我会这样做:
$items = 5; // 5 items per page
$page = $_GET['page']; // current page
$array = json_decode('...', true);
$friends = $array['friends']['data'];
$pages = ceil(count($friends) / $items); // number of pages (total)
$page = $page < 1 ? 1 : $page; // if page is inferior to 1 make it 1
$page = $page > $pages ? $pages : $page; // if is superior to total make it total
$current = array_slice(array_slice($friends, $page-1), 0, $items);
// show friends for current page
foreach($current as $friend) {
echo $friend['id'] . '<br />';
}
// pagination
for($i = 1; $i <= $pages; $i++) {
$class = $i == $page ? ' class="active"' : '';
echo '<a' . $class . ' href="url?page=' . $i . '">' . $i . '</a>';
}