0

I am making web site for my project. Since there are lots of image in my website, I want to display them page by page. Here is the little code snippet from my code:

PHP:

$page_number = 1;
$url_polls = 'http://sitename.herokuapp.com/api/v1/polls/extended/page/' . $page_number;
$string = file_get_contents($url_polls);
$result = json_decode($string, true);

When you increase the page_number, new images will come to the page. In order to increase page_number variable, web page have to be scrolled down. Here is the code snippet for jquery function:

jQuery:

<script type="text/javascript">
   $(window).scroll(function() {
      if($(window).scrollTop() + $(window).height() == $(document).height())   {
         alert("bottom!");
      }
   });
</script>

This script recognizes when reaching at the end of the page by scrolling down. I want to ask couple of questions:

  1. How can I send page_number variable to AJAX in order to increment it in there?
  2. How can I return this page_number variable in order to use in PHP code?
  3. Is there any easy way to increment page_number just by using jQuery code?

Sorry for long post, I am newbie in web-development. Thanks in advance.

  • Check this answer: http://stackoverflow.com/questions/20007721/parsing-returned-html-from-jquery-ajax-request – Agu V Apr 07 '16 at 15:35
  • @AguV this is not I look for. However, thanks for response. – Efe Özelçi Apr 07 '16 at 15:54
  • Sounds like you're looking for something like "lazy loading" http://www.appelsiini.net/projects/lazyload – Mike Apr 07 '16 at 16:00
  • To give a more generalized version of Jake's comment: loading content on scroll is a solved problem. I suggest you google for that rather than trying to cram various web technologies together to sorta kinda get the solution you thought of. You are, to use an idiom, barking up the wrong tree. – Jared Smith Apr 07 '16 at 16:02
  • If you're working on the same domain or are able to configure this API to allow CORS, you can do it entirely on the front end. In any case, Have a counter (global variable) you increment each time your page bottom is reached. This counter would be your page number. using jquery you send a get query to your back end with the counter value as argument – lisa Apr 07 '16 at 16:48
  • @peuh Yeah, I tough the same idea but I could not find any appropriate way to send it to back-end. Can you write little code snippet, if you have an idea about how it handle? – Efe Özelçi Apr 07 '16 at 17:06
  • Infinity loading maybe? http://airbnb.io/infinity/ – Mike Apr 07 '16 at 17:19

0 Answers0