1

Possible to pull random 10 entries (without duplicating entries) with ajax everytime when scroll to the bottom, right now it loads random 10 entires but it duplicates and some entries never shows up. Any ideas? thanks

index.html:

{exp:channel:entries channel="channel_A|channel_B|channel_C" orderby="random" limit="10"}
  {content}
{/exp:channel:entries}

ajax/index.html

{exp:channel:entries channel="channel_A|channel_B|channel_C" orderby="random" limit="10"}
  {content}

  {paginate}
    {pagination_links}
      <ul class="lepaginate">
        {page}
          <li><a href="{pagination_url}" class="page-{pagination_page_number} {if current_page}active{/if}">{pagination_page_number}</a></li>
        {/page}
      </ul>
    {/pagination_links}
  {/paginate}

{/exp:channel:entries}

jquery:

  var counter = 10;

  $(window).scroll(function() {
    if($(window).scrollTop() + $(window).height() == $(document).height()) {


        var response;
        $.ajax({ type: "GET",   
          url: ""+document.location.origin+"/ajax/P"+counter+"",
          async: false,
          success : function(text) {
            response = text;
          }
        });

        counter += 10 ;
        newItems = $(response).appendTo('.grid');
        $grid.isotope('appended', newItems );


          var $grid = $('.grid').isotope({
            masonry: {
              columnWidth: 360,
              gutter: 30
            }
          });

    }

  });

this is using pagination, so it loads like '/ajax/P10' '/ajax/P20' ..., etc

1
  • Have you tried using paginate="hidden"? I'm not sure it works, but maybe it worth to try.
    – Sobral
    Jan 27, 2016 at 21:38

3 Answers 3

2

Two approaches come to mind, the first is to load everything (all results) and only show X at a time (JavaScript) based on your own custom pagination or lazy loading effect. But that means you'd still be sending to the browser all the raw html initially, so might have performance issues.

How channel:entries works with the limit parameter in ExpressionEngine is by running a full search and getting all results back as a delimited list of entry id's based on all your search/filtering options. It then runs a second query to extract all the information it needs on the limited subset of entries to then be able to parse and present the data in your template.

So the second approach could actually be done a few ways depending on your skill level, but the principle is that you do a minimal query returning the entry IDs for all results (pipe delimited), then retain that as a JavaScript variable feeding the parts of the total results to match the (paginated) page results you require - feed that to your ajax call which in turn populates the entries to be shown in your final channel:entries tag.

Although initially you could use a channel:entries tag to get the full list, it should be better performance to use the exp:query tag - depending on the complexity of your filtering/search parameters. You could then post the required entries (for next page) to the ajax URL, and on the resulting page/template use the channel:entries parameter dynamic_parameters to use the POST data to automatically populate the entry_id parameter on the channel:entries tag, therefore only showing the limited entries.

Note that by default channel:entries only pulls back 100 results, if you want to pull more, you have to specify the limit parameter. From memory, going over 1000 starts to cause massive performance issues.

0

You can't meaningfully combine orderby=random and pagination. Basically what you're doing is this each time:

  • Give me 10 random entries
  • Give me 20 random entries and ignore the first 10
  • Give me 30 random entries and ignore the first 20
  • Etc

Hence the duplication - there's no consistency between queries due to the random ordering.

0

You could , with php, get all the entry_ids of the displayed entry. And in your ajax exp:channel:entries entry_id="not YOU ID LIST"

Didn't read you entire post. Then my solution seems impossible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.