使用以下代碼從我的Twitter配置文件中顯示朋友列表。 Id喜歡每次只裝載一定數量,比如20,然後提供在底部的第一1-2-3-4-5(然而,許多通過限制分)最後分頁鏈接如何使用PHP在foreach循環中對行進行分頁
$xml = simplexml_load_string($rawxml);
foreach ($xml->id as $key => $value)
{
$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>";
}
* *****更新******
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>";
這個工作,就必須使輸出電壓偏移開始$i
。思考array_slice
?
這已經被問了很多 - 例如參見http://stackoverflow.com/questions/163809/smart-pagination-algorithm – 2009-11-05 10:10:28
Havnt發現什麼即時尋找。只是如何做到這一點的MySQL結果的例子。 – mrpatg 2009-11-05 10:41:14
我很擔心我錯過了一些東西,爲什麼當你沒有實際循環每個項目時使用foreach循環是很重要的? – MalphasWats 2009-11-05 12:52:35