如何使用PHP对foreach循环中的行进行分页
使用以下代码显示我的twitter个人资料中的好友列表。 我想一次只加载一个特定的数字,比如说20,然后在底部为前1-2-3-4-5(不管多少除以限制)提供分页链接如何使用PHP对foreach循环中的行进行分页,php,pagination,simplexml,Php,Pagination,Simplexml,使用以下代码显示我的twitter个人资料中的好友列表。 我想一次只加载一个特定的数字,比如说20,然后在底部为前1-2-3-4-5(不管多少除以限制)提供分页链接 $xml=simplexml\u load\u字符串($rawxml); foreach($xml->id作为$key=>$value) { $profile=simplexml\u加载\u文件(“https://twitter.com/users/$value”); $friendscreenname=$profile->{“屏幕
$xml=simplexml\u load\u字符串($rawxml);
foreach($xml->id作为$key=>$value)
{
$profile=simplexml\u加载\u文件(“https://twitter.com/users/$value”);
$friendscreenname=$profile->{“屏幕名称”};
$profile\u image\u url=$profile->{“profile\u image\u url”};
回声“
”;
}
******更新******
if (!isset($_GET['i'])) {
$i = 0;
} else {
$i = (int) $_GET['i'];
}
$limit = $i + 10;
$rawxml = OauthGetFriends($consumerkey, $consumersecret, $credarray[0], $credarray[1]);
$xml = simplexml_load_string($rawxml);
foreach ($xml->id as $key => $value)
{
if ($i >= $limit) {
break;
}
$i++;
$profile = simplexml_load_file("https://twitter.com/users/$value");
$friendscreenname = $profile->{"screen_name"};
$profile_image_url = $profile->{"profile_image_url"};
echo "<a href=$profile_image_url>$friendscreenname</a><br>";
}
echo "<a href=step3.php?i=$i>Next 10</a><br>";
if(!isset($\u GET['i'])){
$i=0;
}否则{
$i=(int)$_GET['i'];
}
$limit=$i+10;
$rawxml=OauthGetFriends($consumerkey、$ConsumerCret、$credarray[0]、$credarray[1]);
$xml=simplexml\u load\u字符串($rawxml);
foreach($xml->id作为$key=>$value)
{
如果($i>=$limit){
打破
}
$i++;
$profile=simplexml\u加载\u文件(“https://twitter.com/users/$value”);
$friendscreenname=$profile->{“屏幕名称”};
$profile\u image\u url=$profile->{“profile\u image\u url”};
回声“
”;
}
回声“
”;
这是可行的,只需从
$i
开始偏移输出即可。思考array\u slice
?如果每次都加载完整的数据集,可以非常直接地使用for循环而不是foreach:
$NUM_PER_PAGE = 20;
$firstIndex = ($page-1) * $NUM_PER_PAGE;
$xml = simplexml_load_string($rawxml);
for($i=$firstIndex; $i<($firstIndex+$NUM_PER_PAGE); $i++)
{
$profile = simplexml_load_file("https://twitter.com/users/".$xml->id[$i]);
$friendscreenname = $profile->{"screen_name"};
$profile_image_url = $profile->{"profile_image_url"};
echo "<a href=$profile_image_url>$friendscreenname</a><br>";
}
每页$NUM\u=20;
$FIRSTDINDEX=($page-1)*$NUM_/页;
$xml=simplexml\u load\u字符串($rawxml);
对于($i=$firstIndex;$iid[$i]);
$friendscreenname=$profile->{“屏幕名称”};
$profile\u image\u url=$profile->{“profile\u image\u url”};
回声“
”;
}
您还需要将$i限制为数组长度,但希望您能了解要点。一个非常优雅的解决方案是使用:
这个问题已经被问了很多次了——比如说,我还没有找到我要找的东西。只是一些如何处理mysql结果的例子。我担心我遗漏了一些东西,为什么在没有实际循环每个项目时使用foreach循环很重要?我一直在看这个,但我仍然不知道如何使用它。对于初学者,使用您提供的以“//或更简洁”开头的示例,其中定义了$offset和$count?此外,允许访问者遍历分页数据的可见链接从何而来?这与我想做的非常相似(分页XML输出),我只是不知道从哪里开始使用您提供的示例,如果您不介意用更完整的代码更新您的示例,我将不胜感激。
$NUM_PER_PAGE = 20;
$firstIndex = ($page-1) * $NUM_PER_PAGE;
$xml = simplexml_load_string($rawxml);
for($i=$firstIndex; $i<($firstIndex+$NUM_PER_PAGE); $i++)
{
$profile = simplexml_load_file("https://twitter.com/users/".$xml->id[$i]);
$friendscreenname = $profile->{"screen_name"};
$profile_image_url = $profile->{"profile_image_url"};
echo "<a href=$profile_image_url>$friendscreenname</a><br>";
}
$xml = simplexml_load_string($rawxml);
// can be combined into one line
$ids = $xml->xpath('id'); // we have an array here
$idIterator = new ArrayIterator($ids);
$limitIterator = new LimitIterator($idIterator, $offset, $count);
foreach($limitIterator as $value) {
// ...
}
// or more concise
$xml = simplexml_load_string($rawxml);
$ids = new LimitIterator(new ArrayIterator($xml->xpath('id')), $offset, $count);
foreach($ids as $value) {
// ...
}