连续页面加载内容PHP [英] Continuous page load content PHP
问题描述
当用户滚动浏览我的网页时,我试图一次从数据库中加载10个项目的内容.现在,我可以滚动加载10个项目,但是当它们滚动更多时,如何加载接下来的10个项目.目前,我的php每次都会获取前10个项目,而我的滚动功能仅运行一次.我当时在看jscroll,但不确定它的含义.
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:
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';
}
}
});});
推荐答案
您需要实现分页".这基本上是一个偏移量,也是从该偏移量开始获取X行的限制.如果您通过Google搜索"php分页教程",则会发现很多教程,此处是其中之一.
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.
这篇关于连续页面加载内容PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!