使用Ajax和Jquery的Codeigniter分页无限滚动仅提供重复数据

Din*_*atr 5 javascript ajax jquery codeigniter-3

我想将分页创建为无限滚动。我正在使用codeigniter。但这是在复制数据。因此,请帮助我编辑我的分页代码。我已经挣扎了好几天。如果您能帮助我,我将非常感激。谢谢。当我使用下面的代码

$(window).scrollTop() + $(window).height() == $(document).height()
Run Code Online (Sandbox Code Playgroud)

工作正常

当滚动条到达footerdiv 时我尝试加载时,它仅提供重复数据。

$(window).scrollTop()+$(window).height() >= $('#footerdivid').offset().top
Run Code Online (Sandbox Code Playgroud)

我的代码如下

视图

<html>
    <body>
        <div id="container">
            <h1>Codeigniter Pagination Infinite Scroll</h1>
            <div id="body">
              <ol> <div id="results"></div></ol>
            </div>    
        </div>
    </body>
</html>

<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var total_record = 0;
var total_groups = <?php echo $total_data; ?>;  
$('#results').load("<?php echo base_url() ?>content/load_more",
 {'group_no':total_record}, function() {total_record++;});
$(window).scroll(function() {       
    if($(window).scrollTop()+$(window).height() >= $('#fooerdivid').offset().top)  
    {           
        if(total_record <= total_groups)
        {
          loading = true; 
          $('.loader_image').show(); 
          $.post('<?php echo site_url() ?>content/load_more',{'group_no': total_record},
            function(data){ 
                if (data != "") {                               
                    $("#results").append(data);                 
                    $('.loader_image').hide();                  
                    total_record++;
                }
            });     
        }
    }
});
});
</script>
Run Code Online (Sandbox Code Playgroud)

控制者

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Content extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this -> load->model('content_model');
    }

    public function index()
    {
        $total_data = $this->content_model->get_all_count();
        $content_per_page = 5; 
        $this->data['total_data'] = ceil($total_data->tol_records/$content_per_page);
        $this->load->view('content_page', $this->data, FALSE);
    }

    public function load_more()
    {
        $group_no = $this->input->post('group_no');
        $content_per_page = 5;
        $start = ceil($group_no * $content_per_page);
        $all_content = $this->content_model->get_all_content($start,$content_per_page);
        if(isset($all_content) && is_array($all_content) && count($all_content)) : 
            foreach ($all_content as $key => $content) :
                 echo '<li>'.$content->title.'</li>';
                 echo '<p>'.$content->description.'</p>';                 
            endforeach;                                
        endif; 
    }

}
Run Code Online (Sandbox Code Playgroud)

模型

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Content_model extends CI_Model
{
    public function get_all_count()
    {
        $sql = "SELECT COUNT(*) as tol_records FROM content_information";       
        $result = $this->db->query($sql)->row();
        return $result;
    }

    public function get_all_content($start,$content_per_page)
    {
        $sql = "SELECT * FROM  content_information LIMIT $start,$content_per_page";       
        $result = $this->db->query($sql)->result();
        return $result;
    }

}
Run Code Online (Sandbox Code Playgroud)