0
I am trying to implement an Infinit scroll in my project, but I am having difficulty solving this problem
the connection is perfect and I’m receiving the information from BD, but the code only shows one entry, what I must change to solve this?
Code index.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery Infinite Scroll</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
var start = 0;
var working = false;
$(document).ready(function() {
$.ajax({
type: "GET",
url: "data.php?start="+start,
processData: false,
contentType: "application/json",
data: '',
success: function(r) {
r = JSON.parse(r)
for (var i = 0; i < r.length; i++) {
$('body').append("<div><h1>"+r[i].videoName+"</h1><h2>Video View: "+r[i].videoViews+"</h2></div>")
}
start += 6;
},
error: function(r) {
console.log("Something went wrong!");
}
})
})
$(window).scroll(function() {
if ($(this).scrollTop() + 1 >= $('body').height() - $(window).height()) {
if (working == false) {
working = true;
$.ajax({
type: "GET",
url: "data.php?start="+start,
processData: false,
contentType: "application/json",
data: '',
success: function(r) {
r = JSON.parse(r)
for (var i = 0; i < r.length; i++) {
$('body').append("<div><h1>"+r[i].videoName+"</h1><h2>Video View: "+r[i].videoViews+"</h2></div>")
}
start += 6;
setTimeout(function() {
working = false;
}, 4000)
},
error: function(r) {
console.log("Something went wrong!");
}
});
}
}
})
</script>
</body>
</html>
Data code.php:
<?php
class Video {
public $videoName = "";
public $videoViews = "";
public function __construct($videoName, $videoViews) {
$this->videoName = $videoName;
$this->videoViews = $videoViews;
}
}
$conectar = mysqli_connect("localhost", "***", "***", "***") or die("Erro 001 - Conection lost");
if ($conectar->connect_errno) {
echo "Falha ao conectar com o mysql: (" . $conectar->connect_errno . ") " . $conectar->connect_error;
}
$start = $_GET['start'];
$data = array();
$query = "SELECT * FROM videos ORDER BY id DESC";
$resultado = mysqli_query($conectar, $query);
while($linhas = mysqli_fetch_assoc($resultado)){
$possibleVideos = array(
new Video($linhas['nome'],$linhas['tempo']),
);
}
for ($i = $start; $i < $start+6; $i++) {
if ($i < count($possibleVideos)) {
array_push($data, $possibleVideos[$i]);
}
}
//echo "<pre>";
echo json_encode($data);
?>
Why not use OFFSET of
MySQL
orSELECT * FROM videos ORDER BY id DESC LIMIT $start, 5;
?– Valdeir Psr
can’t do, I was learning in a tutorial until it came to this
– Éo Ronaldo Dll
Try with the code
SELECT ...
that I posted and see if it solves something. With it you nay will need thefor ($i = $start; $i < $start+6; $i++) ...
– Valdeir Psr
unfortunately it did not work, replacing SELECT... and removing the for ($i... the page is blank, does not carry any record
– Éo Ronaldo Dll
Is you are not assigning the values in the variable
$date
. Complete example: https://pastebin.com/HJ37Rram– Valdeir Psr
he is bringing the amount of entries but does not bring the information, all records are listed as Undefined
– Éo Ronaldo Dll
Add
console.log(r);
beforer = JSON.parse(r)
and check on the tabConsole
(F12) what the result is. I changed my code in the link above.– Valdeir Psr
Sensational friend, works perfectly, thank you very much!
– Éo Ronaldo Dll
I came across a problem when I tried to implement the code to the project, the problem is that the entries load at the bottom of the page and not within the <div class="Row"> that the scrypt is found... any idea that can help me? @Valdeirpsr
– Éo Ronaldo Dll
@For this, just create the div where you want to put the element,assign an id to it and then do the following: $('#your-div'). append(result)
– Jorge.M