Php Mysql Ajax无限滚动
我使用下面的代码进行无限滚动,但我得到了2个“错误”。 1.当它到达最后一条记录时,它会再次显示接下来的4条记录 2.当它结束并显示所有记录时,我没有收到消息“No More records” 在我的php文件中,我得到了以下信息:Php Mysql Ajax无限滚动,php,javascript,jquery,Php,Javascript,Jquery,我使用下面的代码进行无限滚动,但我得到了2个“错误”。 1.当它到达最后一条记录时,它会再次显示接下来的4条记录 2.当它结束并显示所有记录时,我没有收到消息“No More records” 在我的php文件中,我得到了以下信息: <script type="text/javascript"> $(document).ready(function(){ $(window).scroll(function() { if ( document.documentEle
<script type="text/javascript">
$(document).ready(function(){
$(window).scroll(function() {
if ( document.documentElement.clientHeight + $(document).scrollTop() >= document.body.offsetHeight )
{
$("#myloaderias").fadeIn("fast");
$.ajax({ //START OF AJAX CALL
url: "loadmore.php?lastComment=" + $(".imgholder:last").attr("id"),
cache: false,
success: function(html){
$("#myloaderias").fadeOut("fast");
if(html){ //IF(HTML)
$("#masterdivholder").append(html); //APPEND RESULTS IN THE DIV THAT HOLDS ALL THE IMAGES
$("#nmc").hide();
} else {
$("#nmc").fadeIn("fast'");
} //END IF(HTML)
}//END SUCCESS FUNCTION
});//END OF AJAX CALL
} //END OF CHECK IF YOU ARE AT THE BOTTOM OF THE PAGE
}); //END OF (window).scroll(function(){
});
</script>
<div id="myloaderias" style="display:none;"><img src="images/ajax-loader.gif"></div>
<div id="loadMoreComments">
<div style="clear:both;"></div>
</div>
<div id="nmc">No More Results</div>
<?php
require "connectiondb.php";
$active = "1";
$mysql_query = mysql_query ("SELECT * FROM gallery WHERE active = '$active' ORDER BY id DESC LIMIT 0,30");
while($pic = mysql_fetch_assoc($mysql_query)) {
echo'
<div class="imgholder" id="'.$pic['id'].'">
<div class="imgcaption">'.$pic['imgtitle'].'</div>
<div class="imgclass">
<a href="www"><img src="'.$pic['thumbpath'].'" width="210" height="140" alt="'.$pic['imgtitle'].'" style="-moz-border-radius: 6px; -webkit-border-radius: 6px;" border="0"/></a>
</div>
<div id="sharebox">
<div style="width:210px; height:32px; float: left; margin-top:3px;">
<div id="socialbox1"><a href="'.$pic['id'].'"><img src="socialicons/facebook.png" width="32" height="32" border="0" /></a></div>
<div id="socialbox"><a href="#"><img src="socialicons/twitter.png" width="32" height="32" border="0" /></a></div>
<div id="socialbox"><a href="#"><img src="socialicons/google.png" width="32" height="32" border="0" /></a></div>
<div id="socialbox"><a href="#"><img src="socialicons/youtube.png" width="32" height="32" border="0" /></a></div>
</div>
</div>
</div>';
}
?>
<?php
require "connectiondb.php";
if($_GET['lastComment']){
$lastcomment = $_GET['lastComment'];
$myquery = mysql_query ("SELECT * FROM gallery WHERE id < '$lastcomment' ORDER BY id DESC LIMIT 0,5");
while($pics = mysql_fetch_assoc($myquery)) {
echo'
<div class="imgholder" id="'.$pics['id'].'">
<div class="imgcaption">'.$pics['imgtitle'].'</div>
<div class="imgclass">
<a href="www"><img src="'.$pics['thumbpath'].'" width="210" alt="'.$pics['imgtitle'].'" border="0" /></a>
</div>
<div id="sharebox">
<div style="width:210px; height:32px; float: left; margin-top:3px;">
<div id="socialbox1"><a href="'.$pics['id'].'"><img src="socialicons/facebook.png" width="32" height="32" border="0" /></a></div>
<div id="socialbox"><a href="#"><img src="socialicons/twitter.png" width="32" height="32" border="0" /></a></div>
<div id="socialbox"><a href="#"><img src="socialicons/google.png" width="32" height="32" border="0" /></a></div>
<div id="socialbox"><a href="#"><img src="socialicons/youtube.png" width="32" height="32" border="0" /></a></div>
</div>
</div>
</div>';
}
}
?>
$(文档).ready(函数(){
$(窗口)。滚动(函数(){
if(document.documentElement.clientHeight+$(document.scrollTop()>=document.body.offsetHeight)
{
$(#myloaderias”).fadeIn(“fast”);
$.ajax({//ajax调用的开始
url:“loadmore.php?lastComment=“+$(“.imgholder:last”).attr(“id”),
cache:false,
成功:函数(html){
美元(“#myloaderias”)。淡出(“快速”);
if(html){//if(html)
$(“#masterdivholder”).append(html);//append生成保存所有图像的DIV
$(“#nmc”).hide();
}否则{
美元(“#nmc”).fadeIn(“快速”);
}//如果结束(HTML)
}//结束成功函数
});//AJAX调用结束
}//检查是否在页面底部结束
});//结束(窗口)。滚动(函数(){
});
没有更多的结果
和检索inde.php中数据的php代码:
<script type="text/javascript">
$(document).ready(function(){
$(window).scroll(function() {
if ( document.documentElement.clientHeight + $(document).scrollTop() >= document.body.offsetHeight )
{
$("#myloaderias").fadeIn("fast");
$.ajax({ //START OF AJAX CALL
url: "loadmore.php?lastComment=" + $(".imgholder:last").attr("id"),
cache: false,
success: function(html){
$("#myloaderias").fadeOut("fast");
if(html){ //IF(HTML)
$("#masterdivholder").append(html); //APPEND RESULTS IN THE DIV THAT HOLDS ALL THE IMAGES
$("#nmc").hide();
} else {
$("#nmc").fadeIn("fast'");
} //END IF(HTML)
}//END SUCCESS FUNCTION
});//END OF AJAX CALL
} //END OF CHECK IF YOU ARE AT THE BOTTOM OF THE PAGE
}); //END OF (window).scroll(function(){
});
</script>
<div id="myloaderias" style="display:none;"><img src="images/ajax-loader.gif"></div>
<div id="loadMoreComments">
<div style="clear:both;"></div>
</div>
<div id="nmc">No More Results</div>
<?php
require "connectiondb.php";
$active = "1";
$mysql_query = mysql_query ("SELECT * FROM gallery WHERE active = '$active' ORDER BY id DESC LIMIT 0,30");
while($pic = mysql_fetch_assoc($mysql_query)) {
echo'
<div class="imgholder" id="'.$pic['id'].'">
<div class="imgcaption">'.$pic['imgtitle'].'</div>
<div class="imgclass">
<a href="www"><img src="'.$pic['thumbpath'].'" width="210" height="140" alt="'.$pic['imgtitle'].'" style="-moz-border-radius: 6px; -webkit-border-radius: 6px;" border="0"/></a>
</div>
<div id="sharebox">
<div style="width:210px; height:32px; float: left; margin-top:3px;">
<div id="socialbox1"><a href="'.$pic['id'].'"><img src="socialicons/facebook.png" width="32" height="32" border="0" /></a></div>
<div id="socialbox"><a href="#"><img src="socialicons/twitter.png" width="32" height="32" border="0" /></a></div>
<div id="socialbox"><a href="#"><img src="socialicons/google.png" width="32" height="32" border="0" /></a></div>
<div id="socialbox"><a href="#"><img src="socialicons/youtube.png" width="32" height="32" border="0" /></a></div>
</div>
</div>
</div>';
}
?>
<?php
require "connectiondb.php";
if($_GET['lastComment']){
$lastcomment = $_GET['lastComment'];
$myquery = mysql_query ("SELECT * FROM gallery WHERE id < '$lastcomment' ORDER BY id DESC LIMIT 0,5");
while($pics = mysql_fetch_assoc($myquery)) {
echo'
<div class="imgholder" id="'.$pics['id'].'">
<div class="imgcaption">'.$pics['imgtitle'].'</div>
<div class="imgclass">
<a href="www"><img src="'.$pics['thumbpath'].'" width="210" alt="'.$pics['imgtitle'].'" border="0" /></a>
</div>
<div id="sharebox">
<div style="width:210px; height:32px; float: left; margin-top:3px;">
<div id="socialbox1"><a href="'.$pics['id'].'"><img src="socialicons/facebook.png" width="32" height="32" border="0" /></a></div>
<div id="socialbox"><a href="#"><img src="socialicons/twitter.png" width="32" height="32" border="0" /></a></div>
<div id="socialbox"><a href="#"><img src="socialicons/google.png" width="32" height="32" border="0" /></a></div>
<div id="socialbox"><a href="#"><img src="socialicons/youtube.png" width="32" height="32" border="0" /></a></div>
</div>
</div>
</div>';
}
}
?>
尝试将async:false添加到ajax调用中。这将解决sinhronization问题
另一个解决方案更为复杂,但在执行下一个ajax调用之前,您需要等待下一组记录。尝试向ajax调用添加async:false。这将解决sinhronization问题
另一个解决方案更复杂,但在执行下一个ajax调用之前,您需要等待下一组记录。您在loadmore上查询。php
没有限制参数,如果$lastcomment
将大于id列
您已从ajax调用
中设置了限制,则它将始终获取5条记录10,5
15,5
等等,没有更多结果的原因
并不流行,因为您在ajax
中的html
变量中得到了一些结果,因此它不会在您的else案例中登陆。请尝试在loadmore.php
中设置查询并使用if-else,如果数据存在于$pics
抱歉@dianuj我没听懂..你能再解释一下吗..我累坏了!!!首先请告诉我是否有可能修复我的记录..谢谢!@dianuj我在你查询的房间loadmore.php
没有限制参数如果$lastcoment
比id列
大,它将始终获取5条记录已经从ajax调用设置了限制,比如10,5
15,5
等等,并且没有更多结果的原因不会消失,因为您在ajax
中的html
变量中得到了一些结果,因此它不会出现在您的else案例中尝试在loadmore.php
如果数据存在于$pics
抱歉@dianuj我没有得到它..你能解释一下吗..我累坏了!!!首先请告诉我是否可以修复我的记录..谢谢!@dianuj我在房间里