简体   繁体   English

从数据库检索Ajax

[英]Ajax retrieving from a database

I am creating a page to get images from the db as you scroll through, but my problem is if you scroll through the following website and get images from the db when someone adds a new image to the db, the next get images will be displayed again. 我正在创建一个页面,以便在滚动浏览时从db获取图像,但是我的问题是,如果您在以下网站上滚动并在有人向db添加新图像时从db获取图像,则将显示下一个获取图像。再次。

How do I prevent it? 我该如何预防?

HTML: HTML:

<div class="row">

    <div class="col-sm-6  rowL">

    </div>

    <div class="col-sm-6  rowR">            

    </div>

</div>

JS: JS:

$(function()
{
    var load = 0;
    $(window).scroll(function(){

        var win =  $(window).height();
        var doc = $(document).height();
        var top = $(window).scrollTop();
        var loadat = doc - win;
        loadat = loadat - 200;

        if(top >= loadat)
        {
            load++;
            $.post('ajax.php', {load: load}, function(data)
            {                            
                var str = data;
                var res = str.split("split");

                //alert(res);

                $(".rowL").append(res[0],res[1],res[2],res[3],res[4]);
                $(".rowR").append(res[5],res[6],res[7],res[8],res[9]);
            });

        }
    });
});

PHP: PHP:

<?PHP
    include 'core/init.php';

    $db = DB::getInstance();
    $img = $db->Get('img'); 


    $load = $_POST['load'];

    $dane = $db->scroll($load);


    foreach($dane as $key=>$value)
    {
        ?>
        <a href="" class="thumbnail">
            <img src="<?PHP echo $value->src; ?>" alt="img"/>
        </a>split
        <?PHP
    }
?>

public function scroll($load)
{
    $load *= 10; 
    $result = $this->_pdo->query("SELECT * FROM img ORDER BY id DESC LIMIT $load,10");
    return $result->fetchAll(PDO::FETCH_OBJ);   
}

That's to be expected. 这是意料之中的。 eg you have the following image IDs coming back from your query. 例如,您从查询中获得以下图像ID。

  42, 39, 37, 5, 4

Let's say your pagination goes by 2 images, so 假设您的分页有2张图片,所以

  42, 39,      37, 5,      4
  ^^^^^^       ^^^^^       ^
  page 1       page 2      page 3

When a new image (id #50, say) gets added, it'll show at at the FRONT of your query results: 添加新图像(例如ID#50)后,它将显示在查询结果的前部:

 50, 42, 39, 37, 5, 4

so now your pages are off: 所以现在您的页面关闭了:

 50, 42,     39, 37,      5, 4
 page 1      page 2       page 3

There's not much you can do to fix this, since you're going through your pages in "most recent first" ordering. 您无法采取任何措施来解决此问题,因为您以“最新的优先”顺序浏览页面。 Adding a new image will naturally make that new image the most recent, and push all the other images down one space. 添加新图像自然会使该新图像成为最新图像,并将所有其他图像向下推一个空格。

You could keep track of what the "first" record was in the results when you started paging, and exclude anything higher than that. 您可以跟踪开始分页时结果中“第一条”记录的内容,并排除高于该记录的内容。

eg 例如

 loading page 1 -> highest image = 42
 loading page 2 -> add "where id <=42" to the query

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM