3

Using the following code to display a list of friends from my twitter profile. Id like to only load a certain number at a time, say 20, then provide pagination links at the bottom for First 1-2-3-4-5(however many divided by limit) Last

$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>";
}

******update******

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>";

This works, just have to offset the output starting at $i. Thinking array_slice?

hakre
  • 193,403
  • 52
  • 435
  • 836
mrpatg
  • 10,001
  • 42
  • 110
  • 169
  • This has been asked quite a lot - see for example http://stackoverflow.com/questions/163809/smart-pagination-algorithm – Dominic Rodger Nov 05 '09 at 10:10
  • Havnt found what im looking for. Just examples of how to do it for mysql results. – mrpatg Nov 05 '09 at 10:41
  • I'm worried I'm missing something, why is it important that you use a foreach loop when you're not actually looping for each item? – MalphasWats Nov 05 '09 at 12:52

2 Answers2

7

A very elegant solution is using a LimitIterator:

$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) {
    // ...
}
hakre
  • 193,403
  • 52
  • 435
  • 836
Stefan Gehrig
  • 82,642
  • 24
  • 155
  • 189
  • I've been looking at this and looking at itbut I still don't understand how to use it. For starters, Using the example you provided beginning with "//or more concise" Where is $offset and $count defined? Furthermore, where would the visible links to allow a visitor to traverse the paginated data come from? This is very similar to what I'd like to do (Paginate XML output), I just don't know where to begin using the examples you've provided and if you wouldn't mind updating your examples with more complete code I for one would be most appreciative. –  Nov 22 '13 at 13:34
2

If you're loading the full dataset every time, you could be pretty direct about it and use a for loop instead of a 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>";
}

You'll also need to limit $i to the array length, but hopefully you get the gist.

MalphasWats
  • 3,255
  • 6
  • 34
  • 40