1

I have a very long foreach that I would like to load in steps as the user scrolls down (infinite scroll), but I've searched for a solution and did not found a way to do that.

my code resembles this:

foreach ($multi_array as $key=>$value){
  echo $value[0]."<br />";
  echo $value[1]."<br />";
  echo $value[2]."<br />";
  echo $value[3]."<br />";
  echo $value[4]."<br />";
  ....
}

the result is very long and I would like to include a infinite scroll instead of the tradition pagination.

I know that effect has to be done in javascript and I allready tried jquery waypoints, but, for some reason, I couldn't get it to work.

user2599347
  • 91
  • 1
  • 10
  • You need to use Ajax so that on reaching bottom of the page your application can fetch next set of records.... I guess this is similar to [question](http://stackoverflow.com/questions/6164767/infinite-scrolling-within-ajax-loaded-page) – Yogesh Jul 19 '13 at 12:12
  • I think it would be more practical a solution that retrieves data from divs instead of external php file. That way I could open and close a div tag every x times the foreach loops. Once the content is divided in divs, the javascript would just keep retrieving those divs when the page reaches bottom. but how ?!? – user2599347 Jul 19 '13 at 13:36

3 Answers3

1

You can't "scroll" with PHP. PHP is a lanugage that is evaluated on the server. The client (who can scroll) will never see any PHP. If you want to scroll, you'd have to use JavaScript.

One approach with JavaScript is to load the contents with AJAX-requests. The link that has been posted in your comments is a good start: Infinite scrolling within ajax-loaded page

Community
  • 1
  • 1
Zim84
  • 3,404
  • 2
  • 35
  • 40
1

After testing various scripts I couldn't get any of the to work.

In the end, i solved the problem this way:

1- created a php script with the foreach and a pagination system that knows where it is via a $_GET variable

2- created a new file with a div on it. created a trigger that knows when the user reaches the end of the page. that trigger does two things: a simple counter that increments with the trigger and an ajax that retrieves the data of the php script and puts it on the div. The counter is so I can increment the page requested, like this:

url: "data.php?page="+counter

simple but it works. I still have to find a way for the php script to tell the javascript when there are no more pages.

user2599347
  • 91
  • 1
  • 10
0

You can't do it with PHP, but you can mix it with Javascript to create the effect of infinite scroll, asking for a piece of the scroll in each iteration.

This jQuery plugin may help you: jQuery WayPoint Infinite Scroll

Miguel G. Flores
  • 802
  • 7
  • 21
  • that was my first try, and probably the best bet, but i could not get it running. Could you give me a working example? – user2599347 Jul 19 '13 at 12:22