Mysqli/Ajax如何在每个请求中加载x个以上的Results
Mysqli / Ajax How to load x more Results each request?
这里我有一个数据库请求:
$query = mysqli_query($db, "
SELECT * FROM posts p INNER JOIN friends f ON
((p.from_user=f.user1 OR p.from_user=f.user2) AND (f.user1='$ownid' OR f.user2='$ownid') AND f.accepted='1')
GROUP BY p.id ORDER BY p.uhrzeit DESC ;
");
还有一个简单的Javascript,用于检查滚动位置。
$(window).scroll(function () {
if ($(this).scrollTop() > 1500) {
}
});
当然,这些只是没有文档准备功能之类的代码段。
我现在的问题是:每次触发滚动功能时,如何通过Ajax请求获得页面加载的前15个结果,然后获得下15个结果?
我如何在实际查询中实现这一点?
谢谢你的每一个回答。如果有遗漏的信息,请请求,我会更新。
编辑我现在做了什么
JS-
var offset = 15,
scrollEnd = false;
$(window).scroll(function(){
if($(window).scrollTop() >= $('.footer').offset().top && typeof scrollEnd !== 'undefined' && !scrollEnd){
alert();
$.ajax({
url: "./ajax/loadmore_homeposts.php",
data : {offset: offset},
beforeSend: function(){
scrollEnd = true;
}
success:function(r){
if(r.data.length){
// + you have result
offset *=2;
// prepare the html and append into the respective container
alert(r);
scrollEnd = false;
}else{
alert("<p>No more result</p>");
$('.result_container').append('<p>No more result</p>');
}
}
})
}
});
PHP
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
$ownid= $_SESSION['SESSION'];
$offset = isset($_GET['offset']) ? $_GET['offset'] : 0;
$limit = 15;
$query=mysqli_query($db,
"
SELECT * FROM posts p INNER JOIN friends f ON
((p.from_user=f.user1 OR p.from_user=f.user2) AND (f.user1='$ownid' OR f.user2='$ownid') AND f.accepted='1')
GROUP BY p.id ORDER BY p.uhrzeit DESC limit $offset,$limit;
");
$data = [];
while(($row = mysqli_fetch_array($result))){
$data[] = $row;
}
echo json_encode( ['data'=>$data]);
?>
您可以使用mysql偏移量和限制。像这个
$sql="SELECT*FROM menuitem LIMIT"$抵消","$items_per_page;
它将查看$items_per_page中的偏移编号行和项目数。
// + in php
$offset = isset($_GET['offset']) ? $_GET['offset'] : 0;
$limit = 15;
$result = mysqli_query("[YOur query] limit $offset,$limit");
$data = [];
while(($row = mysqli_fetch_array($result))){
$data[] = $row;
}
echo json_encode( ['data'=>$data]);
//= in javascript
var offset = 15,scrollEnd = false;
$(window).scroll(function(){
if($(window).scrollTop() >= $('.footer').offset().top && typeof scrollEnd !== 'undefined' && !scrollEnd){
$.ajax({
url: your url
data : {offset: offset},
beforeSend: function(){
scrollEnd = true;
}
success:function(r){
if(r.data.length){
// + you have result
offset *=2;
// prepare the html and append into the respective container
scrollEnd = false;
}else{
$('.result_container').append('<p>No more result</p>');
}
}
})
}
})
相关文章:
- 如何使用url加载程序在webpack中导入多个图像
- 如何在生成下载文件时显示加载动画
- 有没有任何方法可以将控制器从文件加载到ui路由器$stateProvider中
- 无法在通过jQuery的ajax加载的页面中执行javascript
- Emberjs应用程序加载在除Index之外的所有路由上
- 在chrome.tabs.onCreated之后加载HTML页面
- 单击F5时如何停止页面加载
- HTML5音频加载和播放获胜'我不能在iPad上工作
- 跟踪在页面加载时应用内联样式的JavaScript
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- jQuery Lazy加载动画滚动
- Html页面上的多个Base64图像和平滑加载
- 如何创建带有插槽的vue js组件预加载程序
- Webpack/Rect:遵循egghead.io教程,但出现错误:您可能需要一个合适的加载程序来处理此文件类型
- 如何使该数据在所有元素中加载
- Chrome扩展没有't在重新加载之前考虑期权价值
- 使用javascript在Flash中加载外部图像
- Ajax文件加载和<输入>文件加载
- 使用javascript函数在页面初始化后加载jquery
- Mysqli/Ajax如何在每个请求中加载x个以上的Results