Skip to content

Infinite Loop created in pager.php #23

Closed
beaudesigns opened this Issue Dec 6, 2011 · 6 comments

5 participants

@beaudesigns

I've run into a really weird issue, I spent some time working on it but was unable to track it down completely.

The basic issue is this: When you try to add a per_page parameter to the request, in my case, for invoices, and the per_page number is a squared number of the total number of records available, it creates an infinite loop inside of the Recurly_Pager->current() method.

Example: I have 6 invoices for a particular user. I create this request:

$this->invoices = Recurly_InvoiceList::getForAccount(self::$user->storage->id, array('per_page' => 3));

It creates an infinite loop.

If I set the page size to 4, or anything other than a number that can be squared to reach the total invoices, it works fine.

Here's the current code:

  public function current()
  {
    while ($this->_position >= sizeof($this->_objects)) {
      if (isset($this->_links['next'])) {
        $num_objects = sizeof($this->_objects);
        $this->_loadFrom($this->_links['next']);
        $this->_position -= $num_objects;
      }
    }

    return $this->_objects[$this->_position];
  }

The problem happens inside of the while() loop. If we add && sizeof($this->_objects) > 0 to the while(), it partially fixes the problem, however, it creates an extra valid() record that is blank, meaning if we use a foreach(), we'll have a blank record.

I've spent too much time trying to track it down already, if it becomes a big enough issue, I'll have to try to fix it, but hopefully y'all will figure it out before I do.

One thought, is that part of the problem might be that the API returns a rel="next"; link in the header, even if there is no next page. Should that happen? If there's not another page, shouldn't it just leave that out? I think that would simplify the issue considerably.

@blasthaus

I would love to see this resolved so there is no 'next page' link if there IS NO next page. it was driving me crazy as well as i was looping through all the next pages to get a full result set for accounts and subscriptions only to find out the last api call failed to return a result, so i had to add a condition for that.

@quicksketch

I had this problem over in #31. I posted my code over there that attempted to pager results. Can anyone get a pager going that shows X number of results per page?

@blasthaus
@mbeale
mbeale commented Dec 18, 2012

Paging is done automatically for you, there is no need to manually page through the results.

This code will scroll through all Active records

$subscriptions = Recurly_SubscriptionList::getActive();
foreach ($subscriptions as $subscription) {
print "Subscription: $subscription\n";
}

@quicksketch

Paging is done automatically for you, there is no need to manually page through the results.

Yes I know it automatically retrieves additional results, what I needed was to say "show me the second page of 20 results, which start at result 21". I believe what I did 6 months ago is simply continue through the results until I got to the second page, then I started printing out the results. However as you might imagine this is incredibly inefficient; increasingly so as you get more pages.

@austinheap austinheap closed this Jan 28, 2015
@quicksketch

My question on paging the list of results is still open at #31, unrelated to this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.