Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Nerdmonkey

Infinity scroll pagination

Recommended Posts

Boa tarde!

 

Como eu faço para ele para de repetir a mesma consulta ?

Atualmente ele chega no final, executar o load e lista infinitamente os mesmos resultados

 

Página carregada

 

<?php 
require_once('../class/Registry.class.php');
require_once('../class/PDOConnectionFactory.class.php');
require_once('../class/Dao.class.php');
 
$dao = new DAO();
$getProducts = $dao->getAll('dr_prod_dest','*','WHERE status = 1 ORDER BY modelo');
if($getProducts == true):
foreach($getProducts as $Products):
$photo = './im/produtos/' . $Products->imagem;
?>
 
<ul class="ul-float-li-left default-font">
<li class="photo align-center"><img src="<?= $photo; ?>" alt="<?= $Products->modelo; ?>" title="<?= $Products->modelo; ?>"></li>
<li class="model align-center"><?= $Products->modelo; ?></li>
<li class="clear"></li>
<li class="priece no-float align-center right btn"><i>R$ </i><?= number_format($Products->valor,2,',','.') ?></li>
</ul>
 
<?php
endforeach;
else:
?>
 
<p class="not_found">Nehuma produto disponível</p>
 
<?php endif; ?>

 

Chamada ao plugin

 

/**
* Infinity scroll
*/
$('#tbody_list').scrollPagination({
nop     : 9,
offset  : 0,
error   : 'Não há mais produtos',
delay   : 500,
scroll  : true 
});

 

O plugin

 

(function($) {
 
$.fn.scrollPagination = function(options) {
 
var settings = { 
nop     : 10, // The number of posts per scroll to be loaded
offset  : 0, // Initial offset, begins at 0 in this case
error   : 'No More Posts!', // When the user reaches the end this is the message that is
                           // displayed. You can change this if you want.
delay   : 500, // When you scroll down the posts will load after a delayed amount of time.
              // This is mainly for usability concerns. You can alter this as you see fit
scroll  : true // The main bit, if set to false posts will not load as the user scrolls. 
              // but will still load if the user clicks.
}
 
// Extend the options so they work with the plugin
if(options) {
$.extend(settings, options);
}
 
// For each so that we keep chainability.
return this.each(function() { 
 
// Some variables 
$this = $(this);
$settings = settings;
var offset = $settings.offset;
var busy = false; // Checks if the scroll action is happening 
                 // so we don't run it multiple times
 
// Custom messages based on settings
if($settings.scroll == true) $initmessage = 'Scroll for more or click here';
else $initmessage = 'Click for more';
 
// Append custom messages and extra UI
$this.append('<div class="content"></div><div class="loading-bar">'+$initmessage+'</div>');
 
function getData() {
 
// Post data to ajax.php
$.post('./inc/ProdutosHome.inc.php', {
action        : 'scrollpagination',
   number        : $settings.nop,
   offset        : offset,
   
}, function(data) {
 
// Change loading bar content (it may have been altered)
$this.find('.loading-bar').html($initmessage);
 
// If there is no data returned, there are no more posts to be shown. Show error
if(data == "") { 
$this.find('.loading-bar').html($settings.error); 
}
else {
 
// Offset increases
   offset = offset+$settings.nop; 
   
// Append the data to the content div
   $this.find('.content').append(data);
 
// No longer busy! 
busy = false;
} 
 
});
 
} 
 
getData(); // Run function initially
 
// If scrolling is enabled
if($settings.scroll == true) {
// .. and the user is scrolling
$(window).scroll(function() {
 
// Check the user is at the bottom of the element
if($(window).scrollTop() + $(window).height() > $this.height() && !busy) {
 
// Now we are working, so busy is true
busy = true;
 
// Tell the user we're loading posts
$this.find('.loading-bar').html('Carregando produtos');
 
// Run the function to fetch the data inside a delay
// This is useful if you have content in a footer you
// want the user to see.
setTimeout(function() {
 
getData();
 
}, $settings.delay);
 
} 
});
}
 
// Also content can be loaded by clicking the loading bar/
$this.find('.loading-bar').click(function() {
 
if(busy == false) {
busy = true;
getData();
}
 
});
 
});
}
 
})(jQuery);

Compartilhar este post


Link para o post
Compartilhar em outros sites

Tchê, não me leve a mal, mas seria muito interessante você tentar aprender PHP do início, observo sempre os seus tópicos e fica muito claro que você se baseia no copiar/colar, mesmo que você queira viver de copiar/colar, terá que em algum momento da vida adicionar o "pensar" nesta equação. Você sabe fazer uma paginação?

Respostas:

Sim: Então você sabe que existe um LIMIT que deve ser informado e que o seu script não tem isto em parte alguma.

Não: Então comece pesquisando no google: paginação php mysql. Após entender isto, volte e a gente continua.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Imaginei mesmo que fosse isso. A paginação é feita por jQuery logo o arquivo que recebe o load não identifica e nem entrando no seu comentário desnecessário, muito obrigado pela ajuda.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Você achou e não testou? Como eu disse, copiar/colar sem pensar, não funciona.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Você não está errado. Realmente peguei e colei, e nem tinha parada pra estudar o código.


O problema é que mesmo com a paginação ele fica em loop...

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.