Infinite scroll - controller

Hi to all

I have made infinite scroll for page where is placed filters.
and when i’m filter something its filter good but if I scroll page down it start loads not filtered items to loop

return function($site, $pages, $page) {

  // get all articles and add pagination
  $bookmakers = page('review')->children()->visible();
  $count = $bookmakers->count();
	
  // check if the request is an Ajax request and if the limit and offset keys are set
  if(r::ajax() && get('offset') && get('limit')) {

// convert limit and offset values to integer
$offset = intval(get('offset'));
$limit  = intval(get('limit'));

// limit projects using offset and limit values
$bookmakers = $bookmakers->offset($offset)->limit($limit);

// check if there are more projects left
$more = $count > $offset + 1;

  // otherwise set the number of projects initially displayed
  } else {


  
  if($query_rating = get('rating')) {
	  $bookmakers = $bookmakers->filter(function($bookmaker) use ($query_rating) {
		    $collection = page('user-reviews')->children()->visible()->filterBy('bookmaker_name', '==', $bookmaker->slug());
		    $rating = $collection->pluck('rating_10',',', false);
		    if(array_intersect($query_rating, array(round(a::average($rating))))) {
			   return $bookmaker;
			}
	  });
	  $bookmakers = $bookmakers->sortBy($query_rating, 'asc');	  
	}
  if($query_bookmaker_name = get('bookmaker_name')) {
  		$bookmakers = $bookmakers->search($query_bookmaker_name, 'title');
	}	
  if($query_market = get('market')) {
  		$bookmakers = $bookmakers->search($query_market, 'market');
	}
  if($query_country = get('country')) {
  		$bookmakers = $bookmakers->search($query_country, 'excluded_countries');
	}
  if($query_payment = get('payment')) {
  		$bookmakers = $bookmakers->search($query_payment, 'deposit|withdrawal');
	}
  if($query_language = get('language')) {
  		$bookmakers = $bookmakers->search($query_language, 'website_language');
	}
  if($query_currency = get('currency')) {
  		$bookmakers = $bookmakers->search($query_currency, 'currency');
	}

$offset   = 0;
$limit    = 20;
$bookmakers = $bookmakers->limit($limit);

  }

  // pass $articles and $pagination to the template
  return compact('offset', 'limit', 'bookmakers', 'more', 'bookmaker', 'rating', 'bookmaker_name', 'query_bookmaker_name', 'query_market', 'query_rating', 'query_country', 'query_payment', 'query_language', 'query_currency');

};

where is my mistake?

You use an if statement, where you check if you have an Ajax call, and if not, you do all sorts of filtering. But those filters are not respected in the Ajax call.

You mean that I need to separate controllers? and filter trough other controller (on other page)?

No, that’s not what I meant. I mean, you need to apply the filters also within the Ajax call. But you could in fact use content representations instead of checking for the Ajax call.

Don’t understand about “content representations” how it will helps me?

Can you show where I need to make the changes (just on one filter for example)

All you need to do is use the same filtering code also within the if-statement

if(r::ajax() && get('offset') && get('limit')) {

// convert limit and offset values to integer
$offset = intval(get('offset'));
$limit  = intval(get('limit'));

// use all filters here as well
if($query_bookmaker_name = get('bookmaker_name')) {
  		$bookmakers = $bookmakers->search($query_bookmaker_name, 'title');
	}

// limit projects using offset and limit values
$bookmakers = $bookmakers->offset($offset)->limit($limit);


// check if there are more projects left
$more = $count > $offset + 1;

  // otherwise set the number of projects initially displayed
  } else {

Ive made this changes

return function($site, $pages, $page) {

// get all articles and add pagination
$bookmakers = page('review')->children()->visible();
$count = $bookmakers->count();

// check if the request is an Ajax request and if the limit and offset keys are set
if(r::ajax() && get('offset') && get('limit')) {

// convert limit and offset values to integer
$offset = intval(get('offset'));
$limit  = intval(get('limit'));

	if($query_rating = get('rating')) {
	  $bookmakers = $bookmakers->filter(function($bookmaker) use ($query_rating) {
		    $collection = page('user-reviews')->children()->visible()->filterBy('bookmaker_name', '==', $bookmaker->slug());
		    $rating = $collection->pluck('rating_10',',', false);
		    if(array_intersect($query_rating, array(round(a::average($rating))))) {
			   return $bookmaker;
			}
	  });
	  $bookmakers = $bookmakers->sortBy($query_rating, 'asc');	  
	}
	if($query_bookmaker_name = get('bookmaker_name')) {
			$bookmakers = $bookmakers->search($query_bookmaker_name, 'title');
	}	
	if($query_market = get('market')) {
			$bookmakers = $bookmakers->search($query_market, 'market');
	}
	if($query_country = get('country')) {
			$bookmakers = $bookmakers->search($query_country, 'excluded_countries');
	}
	if($query_payment = get('payment')) {
			$bookmakers = $bookmakers->search($query_payment, 'deposit|withdrawal');
	}
	if($query_language = get('language')) {
			$bookmakers = $bookmakers->search($query_language, 'website_language');
	}
	if($query_currency = get('currency')) {
			$bookmakers = $bookmakers->search($query_currency, 'currency');
	}


// limit projects using offset and limit values
$bookmakers = $bookmakers->offset($offset)->limit($limit);

// check if there are more projects left
$more = $count > $offset + 1;

  // otherwise set the number of projects initially displayed
} else {

	if($query_rating = get('rating')) {
	  $bookmakers = $bookmakers->filter(function($bookmaker) use ($query_rating) {
		    $collection = page('user-reviews')->children()->visible()->filterBy('bookmaker_name', '==', $bookmaker->slug());
		    $rating = $collection->pluck('rating_10',',', false);
		    if(array_intersect($query_rating, array(round(a::average($rating))))) {
			   return $bookmaker;
			}
	  });
	  $bookmakers = $bookmakers->sortBy($query_rating, 'asc');	  
	}
	if($query_bookmaker_name = get('bookmaker_name')) {
			$bookmakers = $bookmakers->search($query_bookmaker_name, 'title');
	}	
	if($query_market = get('market')) {
			$bookmakers = $bookmakers->search($query_market, 'market');
	}
	if($query_country = get('country')) {
			$bookmakers = $bookmakers->search($query_country, 'excluded_countries');
	}
	if($query_payment = get('payment')) {
			$bookmakers = $bookmakers->search($query_payment, 'deposit|withdrawal');
	}
	if($query_language = get('language')) {
			$bookmakers = $bookmakers->search($query_language, 'website_language');
	}
	if($query_currency = get('currency')) {
			$bookmakers = $bookmakers->search($query_currency, 'currency');
	}

$offset   = 0;
$limit    = 20;
$bookmakers = $bookmakers->limit($limit);

}

  // pass $articles and $pagination to the template
return compact('offset', 'limit', 'bookmakers', 'more', 'bookmaker', 'rating', 'bookmaker_name', 'query_bookmaker_name', 'query_market', 'query_rating', 'query_country', 'query_payment', 'query_language', 'query_currency');

};

But this isn’t help. It is still loads all items after scroll (ajax event).

I think that the problem is that inside ajax get info from filter is losing (because im sending filter with POST method)

Yes, I just thought you have to send the filters with the Ajax call as well.

I create JS ajax call for form

// Filter form
$("#bookmaker-form").submit(function(e) {

var url = "/review"; // the script where you handle the form input.

$.ajax({
       type: "GET",
       url: url,
       data: $("#bookmaker-form").serialize(), // serializes the form's elements.
       success: function(data)
       {
           alert(data); // show response from the php script.
       }
     });

e.preventDefault(); // avoid to execute the actual submit of the form.
});

And still no luck with it

https://99bookmakers.com/review
here is how filter works…

All I can create is ugly way to change limit for filtered content. If it is just listing it limits 20 items, if it filtered content it limits by 200 items…

return function($site, $pages, $page) {

// get all articles and add pagination
$bookmakers = page('review')->children()->visible();
$count = $bookmakers->count();
$offset   = 0;
$limit    = 20;

if($query_rating = get('rating')) {
  $bookmakers = $bookmakers->filter(function($bookmaker) use ($query_rating) {
	    $collection = page('user-reviews')->children()->visible()->filterBy('bookmaker_name', '==', $bookmaker->slug());
	    $rating = $collection->pluck('rating_10',',', false);
	    if(array_intersect($query_rating, array(round(a::average($rating))))) {
		   return $bookmaker;
		}
  });
  $bookmakers = $bookmakers->sortBy($query_rating, 'asc');
  $offset   = 0;
  $limit    = 200;
}
if($query_bookmaker_name = get('bookmaker_name')) {
		$bookmakers = $bookmakers->filter(function($bookmaker) use ($query_bookmaker_name) {
		return in_array($bookmaker->slug(), $query_bookmaker_name);	
  });
  
  $offset   = 0;
  $limit    = 200;		
}	
if($query_market = get('market')) {
		$bookmakers = $bookmakers->search($query_market, 'market');
  $offset   = 0;
  $limit    = 200;
}
if($query_country = get('country')) {
		$bookmakers = $bookmakers->search($query_country, 'excluded_countries');
  $offset   = 0;
  $limit    = 200;
}
if($query_payment = get('payment')) {
		$bookmakers = $bookmakers->search($query_payment, 'deposit|withdrawal');
  $offset   = 0;
  $limit    = 200;
}
if($query_language = get('language')) {
		$bookmakers = $bookmakers->search($query_language, 'website_language');
  $offset   = 0;
  $limit    = 200;
}
if($query_currency = get('currency')) {
		$bookmakers = $bookmakers->search($query_currency, 'currency');
  $offset   = 0;
  $limit    = 200;
}



// check if the request is an Ajax request and if the limit and offset keys are set
if(r::ajax() && get('offset') && get('limit')) {

// convert limit and offset values to integer
$offset = intval(get('offset'));
$limit  = intval(get('limit'));


// limit projects using offset and limit values
$bookmakers = $bookmakers->offset($offset)->limit($limit);

// check if there are more projects left
$more = $count > $offset + 1;

// otherwise set the number of projects initially displayed
} 

$bookmakers = $bookmakers->limit($limit);


  // pass $articles and $pagination to the template
return compact('offset', 'limit', 'bookmakers', 'more', 'bookmaker', 'rating', 'bookmaker_name', 'query_bookmaker_name', 'query_market', 'query_rating', 'query_country', 'query_payment', 'query_language', 'query_currency');

};