8
I want to make an automatic page like Facebook, which when it reaches the bottom of the page automatically loads another page with more posts. I’ve already researched the subject and I haven’t found anything that could really help me to the point where I can.
The code I use to list posts is this:
<?php
$post = $pdo->query("SELECT * FROM postagens")->fetchAll();
if(!$post){
print_r($pdo->errorInfo());
}
foreach ($post as $posts){
?>
I tried to do what @abfurlan said and apparently I did something wrong.
The page index.php
was like this:
<?php require_once"../conexao.php"?>
<style>
#conteudo{
width:100px; background:#CCC;
height:200px;
overflow-y:auto;
}
</style>
<script src="JS/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#conteudo").scroll(function() {
if ($(this).scrollTop() + $(this).height() == $(this).get(0).scrollHeight) {
//requisição ajax para selecionar postagens
$.ajax({
url:'postagens.php', //Página PHP que seleciona postagens
type:'POST', // método post, GET ...
data: 'limit=5&offset=0', //seus paramêtros
success: function(data){ // sucesso de retorno executar função
$('#conteudo').append(data); // adiciona o resultado na div #conteudo
} // fim success
}); // fim ajax
} // fim do if
}); // fim scroll
}); // fim document ready
</script>
<div id="conteudo">
</div>
And the page postagens.php
was like this:
<?php require_once "../conexao.php" ?>
<?php
$post = $pdo->query("SELECT * FROM postagens LIMIT 5 OFFSET 0")->fetchAll();
if(!$post){
print_r($pdo->errorInfo());
}
foreach ($post as $posts){
?>
<?php echo $posts['ID']; echo "<br>"?>
<?php } ?>
Nothing is going on...
I think the question does not sit well in "automatic page", I suggest changing the title to "load content according to scroll" or something like that
– Leonardo Bosquett
See if this post is useful, I believe this is what you are looking for: How to load content on demand using jQuery
– Hicharles