0
votes

I'm following the tutorial from "https://www.youtube.com/watch?v=V_qZOaGSEQE". I want to implement auto pagination in phalcon with jquery. auto scroll is working properly without any error but the problem is I don't understand how to pass query data to jquery with phalcon. that's why always 5 data is rendering continuously and its same data. I mean always rendering 1-5 again and again. someone please help me...! how could I retrieve data properly with auto pagination.

[ Controller ]

namespace Multiple\Frontend\Controllers;
use Phalcon\Paginator\Adapter\Model as paginator;

public function postloaderAction()
 {
    $numberPage = $this->request->getPost("page", "int",1);
    $record_per_call = 5;
    $offset = ($numberPage - 1) * $record_per_call;
    $bloger = Blogs::find(array("limit" => array("number" => $record_per_call, "offset" => $offset), "order" => "datetime DESC"));
    foreach($bloger as $blog)
    {
        echo('<div> <h1><a href="blog/details/'.$blog->id.'">'.$blog->blog_title.'</a></h1><img src="uploads/blogs/'.$blog->blog_image.'"/></div>');
    }
 }

 public function indexAction()
 {
    if($this->request->isAjax())
    { 
        echo($this->view->getRender('blog', $this->postloader())); 
        return false;
    }
 }

[ 'Main Layout View / Jquery' ]

var page = 1;
mycontent(page);
$(window).scroll(function(){
   if($(window).scrollTop() + $(window).height() > $('.blogItems').height()) 
   {
    page++;
    mycontent(page);
   }
});
function mycontent(page)
{
    $('.loader_msg').html('Loading Expansion...');
    $.post('blog/postloader',{page:page}, function(data){
        if(data.trim().length == 0)
        {
          $('.loader_msg').html('No Data Found');  
        }
        $('.blogItems').append(data);
       // $('.animate').animate({scrollTop:$('.loading').offset().top}, 1000);
        $('.loader_msg').hide();  
    });
}

[ Blog View ]

<div class="blogItems">
    <p class="loader_msg"></p>
</div>

[ Output ]

enter image description here

1

1 Answers

0
votes

This is just because every time you are trying to get data with a limit in find will give you first 5 records every time, You have to give limit as well as offset to get next n results, and in Phalcon there is no direct way to give a limit and offset so you have to code in this way.

<?php
public function postloaderAction()
{
    $numberPage = $this->request->getPost("page", "int",1);
    $record_per_call = 5;
    $offset = ($numberPage - 1) * $record_per_call;
    $bloger = Blogs::find(array("limit" => array("number" => $record_per_call, "offset" => $offset), "order" => "datetime DESC"));
    foreach($bloger as $blog)
    {
        echo('<div> <h1><a href="blog/details/'.$blog->id.'">'.$blog->blog_title.'</a></h1><img src="uploads/blogs/'.$blog->blog_image.'"/></div>');
    }
}

This will surely work for you.