I'm trying to implement an infinit scroll in my project, but I'm having trouble solving this problem
The connection is perfect and I'm getting the DB information, but the code only shows one entry, what should I change to resolve 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><scripttype="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.php code:
<?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);
?>