Mysqli/Ajax如何在每个请求中加载x个以上的Results

Mysqli / Ajax How to load x more Results each request?

本文关键字:加载 Results 请求 Ajax Mysqli      更新时间:2023-09-26

这里我有一个数据库请求:

$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>');
                        }
            }
            })
            }
            })