Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get "cursor" value to access "next" page and how to access nth page ? #208

Closed
vishalmelmatti opened this issue Feb 29, 2016 · 3 comments
Labels
V2 V2 Client

Comments

@vishalmelmatti
Copy link

Hi,

$accountList = Recurly_AccountList::get($options);

I am fetching first page of accounts list by passing parameter per_page=10. I am getting Recurly_AccountList object.

As per your documentation, to access next page there is cursor parameter with "next" link.

I am trying to var_dump $accountList->getLinks() but it gives me empty array. I know there are hundreds of active accounts.

1 ) How do I get cursor value? I need to get this value so that it could be returned to my single page app which can in turn pass it to my backend to get next page ?

  1. How do I get nth page ? lets say I want to access 7th page directly how should I access it ?

Thanks

@drewish
Copy link

drewish commented Mar 1, 2016

Try:

$account_list = Recurly_AccountList::get(array('per_page' => 10));
$account_list->rewind();
var_dump($account_list->getLinks());

Frustratingly there's not a good way to jump all the way to the 7th page without fetching each intermediate page. The way the PHP pager (as opposed to the other clients) was designed, it hides the underlying pages exposing only the items in each page. Because the Pager is used as a base class for lists of items, rather than just using something simple like arrays, it's tightly coupled with the XML parsing. That makes it really hard to change it to expose pages of items in a way that's backwards compatible with current code :(

I'm wondering if the least bad thing to do would be to add a method to Recurly_Pager that was something like:

  public function nextPage() {
    if (empty($this->_links['next'])) {
      return false;
    }

    $this->_loadFrom($this->_links['next']);
    $this->_position = 0;

    return $this->valid();
  }

So you could do something like this to skip to a specific page and fetch its records:

$per_page = 10;
$page_number = 4;
$invoice_list = Recurly_InvoiceList::get(array('per_page' => $per_page));

// Advance to the desired page
$i = 0;
$invoice_list->rewind();
while (++$i < $page_number) {
  $invoice_list->nextPage()
}

// Extract the elements from the page.
$invoices = array();
$i = 0;
while ($i++ < $per_page && $invoice_list->valid()) {
  $invoices[] = $invoice_list->current();
  $invoice_list->next();
}

@vishalmelmatti
Copy link
Author

Thanks for the reply, will try this out.

@aaron-junot
Copy link

It appears this was a question and that it has since been answered. @vishalmelmatti if you are still experiencing problems, feel free to continue the conversation or open another issue. The above advice is still the best way to get the nth page of results for the current version of the API.

@bhelx bhelx added the V2 V2 Client label Mar 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
V2 V2 Client
Projects
None yet
Development

No branches or pull requests

4 participants