0

I am trying to load content from my database 10 items at a time as the user scrolls through my web page. Right now I can load 10 items on scroll, but how can I load the next 10 items when they scroll more. Currently my php will grab the first 10 items each time, and my scroll function is only running once. I was looking at jscroll but was not really sure about its implimentation.

Here is my code, help appreciated.

PHP:

<?php // Create connection $con=mysqli_connect("localhost","root","","test"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $result = mysqli_query($con,"SELECT * FROM test"); $row = mysqli_fetch_array($result); for ($index=0; $index < 10; $index++) { echo '<table border="0" width="600px">'; echo "<tr>"; echo "<td><p>" . '<img src="' . $row['image'] . '" hspace="10" border="1" style="float:left;">' . "</p>"; echo "<p>" . "#" . $row['ID'] . ": " . $row['confession'] . "</p></td>"; echo "</tr>"; echo "</table>"; echo "<br>"; } mysqli_close($con); ?> 

JavaScript:

$(document).ready(function () { var flg='1'; $(window).scroll(function () { console.log($(document).scrollTop()); if ($(document).scrollTop() >299){ if(flg=='1') { $.post('test2.php', function(data) { $('#content').html(data); }); flg='0'; } } });}); 

2 Answers 2

0

You need to implement "pagination". This is basically an offset and a limit to fetch X rows starting at the offset. If you google for "php pagination tutorial" you'll find plenty of tutorials, here is one of them.

I think describing the whole concept here in detail with code examples would become a very long answer and there are plenty of tutorials, so just check them out.

Sign up to request clarification or add additional context in comments.

2 Comments

No the answer but it's a good place to start. That tutorial got me running the pagination on my site. However I have one problem. When I click the link to page two it loads a blank page and echos all the data onto that blank page. How can I simply update the div the current data is being echoed into? or is there a better way?
You have to make an AJAX call when the link is clicked and replace your page content with the response from the webserver.
0

You need to send a parameter to the server script telling it where to start from. You can then use ORDER BY and LIMIT to select the appropriate records in the database to add.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.