无限滚动-获取动态更新内容的Div ID


Infinite Scrolling - Get the Div ID of Dynamically Updated Content

所以我要为我的博客创建无限滚动。它实际上应该在滚动后拉出一个帖子。我要通过Ajax来实现它。问题是,Ajax是张贴相同的post_id,我得到一个滚动后。我用firebug调试了它。例如:已经有一个post-id = 10的帖子。在我滚动之后,它添加了post-id = 11的下一篇文章。但是在此之后滚动,它只附加post-id = 11的帖子,它不附加post-id = 12的帖子,以此类推。有什么问题吗?这个(发布:最后)选择器有问题吗?如何获得最后一次动态更新内容的id ?

这是我的Index.php:

<?php
include 'resources/init.php';
function get_posts($post_id = null, $tag_id = null){
    $posts = array();
    $query = "SELECT `posts`.`post_id` AS `posts_id`, `tags`.`tag_id` AS `tags_id`,
                        `title`, `txt_content`, `img_content`, `date_posted`,`tag_name`
                        FROM `posts`
                        INNER JOIN `tags` ON `tags`.`tag_id` = `posts`.`tag_id`";
    if(isset($post_id)){
        $post_id = (int)$post_id;
        $query .= " WHERE `posts`.`post_id` = $post_id";
    }
    if(isset($tag_id)){
        $tag_id = (int)$tag_id;
        $query .= " WHERE `tags`.`tag_id` = $tag_id";
    }
    $query .= " ORDER BY `posts`.`post_id` DESC LIMIT 0,1";
    $query = mysql_query($query);
    while($row = mysql_fetch_assoc($query)){
        $posts[] = $row;
    }
    return $posts;
}
$posts = (isset($_GET['post_id'])) ? get_posts($_GET['post_id']) : get_posts();
?>
<!DOCTYPE html>
<html>
<head>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<a href = "http://localhost/simpleblog/add_post.php">Add a Post</a> | <a href = "http://localhost/simpleblog/add_tag.php">Add a Tag</a> | <a href = "http://localhost/simpleblog/tag_list.php">Show Existing Tags</a> | <a href = ""></a>
<p>
<div id = "AddPosts">
<?php
foreach($posts as $post){
?>
    <div class = "Posted" id = "<?php echo $post['posts_id']; ?>">
    <h2><a href = "http://localhost/simpleblog/looks/<?php echo $post['posts_id']; ?>-<?php $strings = array(" "); echo str_replace($strings, '-', preg_replace('/[^a-zA-Z0-9 s]/', '', $post['title'])); ?>"><?php echo $post['title'];?></a></h2>
    <div><?php echo nl2br($post['txt_content']);?></div>
    <div><img src = "http://localhost/simpleblog/<?php echo $post['img_content'];?>"></div>
        <p>Posted on <i><?php echo date("F dS',' Y", strtotime($post['date_posted'])); ?></i>
         in <a href = "http://localhost/simpleblog/tags/<?php echo $post['tags_id'];?>"><?php echo $post['tag_name']; ?></a></p>
         <div><a href = "http://localhost/simpleblog/edit_my_looks/<?php echo $post['posts_id'];?>">Edit This Post</a> |
        <a href = "http://localhost/simpleblog/delte_my_post/<?php echo $post['posts_id'];?>"> Delete This Post</a></div>
    </div>
<?php
}
?>
</div>
<center><img src = "loader.gif" id = "loading-img" width = "200" height = "200" style = "display:none" /></center>
<script>
$(window).scroll(function(){
    if ($(window).scrollTop() == $(document).height() - $(window).height()){
        $('#loading-img').show();
        var post_id = $('.Posted:last').attr('id');
        $.post("add_more_posts.php", {post_id: post_id} , function(data){
            if(data){
                $('#loading-img').fadeOut();
                $('#AddPosts').append(data);
            }else{
                $('#AddPosts').append('Finished Loading!');
            }
        });
    }
});
</script>
</body>
</html>

Add_more_posts.php

<?php
include 'resources/init.php';
function load_more_posts($post_id = null, $tag_id = null){
    $posts = array();
    $query = "SELECT `posts`.`post_id` AS `posts_id`, `tags`.`tag_id` AS `tags_id`,
                        `title`, `txt_content`, `img_content`, `date_posted`,`tag_name`
                        FROM `posts`
                        INNER JOIN `tags` ON `tags`.`tag_id` = `posts`.`tag_id`";
    if(isset($post_id)){
        $post_id = (int)$_POST['post_id'];
        $query .= " WHERE `posts`.`post_id` < $post_id";
    }
    if(isset($tag_id)){
        $tag_id = (int)$tag_id;
        $query .= " WHERE `tags`.`tag_id` = $tag_id";
    }
    $query .= " ORDER BY `posts`.`post_id` DESC LIMIT 0,1";
    $query = mysql_query($query);
    while($row = mysql_fetch_assoc($query)){
        $posts[] = $row;
    }
    return $posts;
}
$posts = (isset($_POST['post_id'])) ? load_more_posts($_POST['post_id']) : load_more_posts();
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
foreach($posts as $post){
?>
    <h2><a href = "http://localhost/simpleblog/looks/<?php echo $post['posts_id']; ?>-<?php $strings = array(" "); echo str_replace($strings, '-', preg_replace('/[^a-zA-Z0-9 s]/', '', $post['title'])); ?>"><?php echo $post['title'];?></a></h2>
    <div><?php echo nl2br($post['txt_content']);?></div>
    <div><img src = "http://localhost/simpleblog/<?php echo $post['img_content'];?>"></div>
        <p>Posted on <i><?php echo date("F dS',' Y", strtotime($post['date_posted'])); ?></i>
         in <a href = "http://localhost/simpleblog/tags/<?php echo $post['tags_id'];?>"><?php echo $post['tag_name']; ?></a></p>
         <div><a href = "http://localhost/simpleblog/edit_my_looks/<?php echo $post['posts_id'];?>">Edit This Post</a> |
        <a href = "http://localhost/simpleblog/delte_my_post/<?php echo $post['posts_id'];?>"> Delete This Post</a></div>
<?php
}
?>
</body>
</html>

好了,下面是为什么它可能不起作用的原因。

在ajax站点上,跟踪获取的id可能会出现问题。如果您正在获取id=12的记录。你是如何记录这些记录的?所以你的问题可能是ajax发送相同的id一遍又一遍地到服务器,这就是为什么它不获取下一个记录。

你可以在javascript中创建静态变量

function static_vars(){
    this.id=0;
}

之后你可以从任何函数

调用它
static_vars.id

所以一旦你获取记录12更新static_vars。Id = 13,并将Id发送给服务器以获取记录13。

static_vars增量。