Search code examples
phppropeloffsetpagination

Propel paginate with newly added items (setting offset manually or sth.)


I have a page where I render items from my database in reversed chronological order. Last added item is the first item on top of the page.

I use infinite scrolling, so everytime the user hits the bottom, I send pagenumber and resultcount to my server. There I use propel paginate to get the next page.

The problem is, when an user adds a new item on the page it will be added to my database and is the first item on page 1 in my results. So all other results are shifted by 1 position. Is there a way to tell propel that it should start at offset+1 to ignore the new added element?

A little example:

I have 5 items per page. In this example every item has only a number, so on the page you can see 1,2,3,4,5. Now, a user adds the item 0. My database results w/o pagination is now: 0,1,2,3,4,5 because the new item is added. Now the user hits the bottom of the page and the pagination has the attributes page = 2, items per page = 5 and I will get 5,6,7,8,9. Now my page renders the 5 again. If I could say propel that the initial offset should start after the first item, the result would be ok.


Solution

  • Just an idea, haven't tried the code myself, but you could add a timestamp to the search criteria and pass it in with the paging so that all search results are restricted to records created before the current search began:

    <?php
    $page = (isset($_GET['page']))?$_GET['page']:1;
    $timeFilter = (isset($_GET['time']))?$_GET['time']:date("Y-m-d H:i:s", time());
    
    $posts = PostQuery::create()
      ->filterByCreatedAt($timeFilter, Criteria::LESS_THAN)
      // add other filters as necessary
      ->paginate($page, $limit);
    // Obviously there is a lot of other things to include, just an example...
    ?>
    <html>
    <head>
      <title>Paging Example</title>
    </head>
    <body>
      <ul id='results'>
        <?php foreach ($posts as $post) {
    
          echo "<li>".$post->getTitle()."</li>";
    
        } ?>
      </ul>
    
      <div id='paging'>
        <?php
        if (!$pager->isFirstPage()) {
          echo "<a href='thisFile.php?page=".$pager->getPreviousPage()."&time=".$timeFilter."'>Previous Page</a>";
        }
    
        // may want to add a page list here...
    
        if (!$pager->isLastPage()) {
          echo "<a href='thisFile.php?page=".$pager->getNextPage()."&time=".$timeFilter."'>Next Page</a>";
        }
    
        ?>
      </div>
      </body>
    </html>