I am generating an AJAX data loading system when scrolling, just like facebook does.
But what happens is that it does not work as I expect, although the code I see very well written and with the corresponding logic.
This is the index.php code:
<?php
//Crear Variables para conexion Noticias Principales
$host = "localhost";
$user = "kautivai_EditorDeNoticias";
$pw = "0927410654marvin";
$dataBase1 = "kautivai_DatosDeNoticias";
$conexion = mysqli_connect($host, $user, $pw, $dataBase1) or die("Problemas al conectar con base de datos 'kautivai_DatosDeNoticias'");
$obtener_filas = mysqli_query($conexion, "SELECT * FROM Noticia1");
$numero_de_fila = mysqli_num_rows($obtener_filas);
// Ejecutas las consulta
$result = mysqli_query($conexion, "SELECT * FROM Noticia1 WHERE ID = '$numero_de_fila'") or die("Error al realizar consulta: ".mysqli_error($conexion));
while($final = mysqli_fetch_array($result)){
?>
<script type="text/javascript">
<?php
echo "var Numero = ".$numero_de_fila.";";
?>
console.log(Numero);
let acum = -1;
let acumulador = () => {
acum++
console.log(acum);
}
var pagina = 7;
var regulador = Numero - pagina;
$(document).ready(function()
{
// Carga inicial
cargardatos();
});
function cargardatos(){
// Petición AJAX
acumulador();
if (acum >= Numero) {
console.log("error")
} else {
$("#loader").html("<img src='imagenes/loading.gif' style='width:135px; height:100px;'>");
$.get("datos.php?Numero="+Numero+"®ulador="+regulador,
function(data){
if (data != "") {
$(".mensaje:last").after(data);
}
$('#loader').empty();
}
);
}
}
$(window).scroll(function(){
if ($(window).scrollTop() == $(document).height() - $(window).height()){
pagina += 10;
cargardatos();
}
});
</script>
<div id="ForLoadMore1">
<div class="mensaje"></div>
</div>
<div id="loader"></div>
This code what it does is show me information brought from the database, loading it in the div class="message", passing variables by $ .get, which are number and regulator, number sends the number of current rows in the table and regulator send the result of (Number - page), page gets the value "7" because I want to load the data from row 8 .. this data is sent to the file datos.php, whose code is the next:
<?php
//Crear Variables para conexion Noticias Principales
$host = "localhost";
$user = "kautivai_EditorDeNoticias";
$pw = "0927410654marvin";
$dataBase1 = "kautivai_DatosDeNoticias";
$Numero = $_GET['Numero'];
$regulador = $_GET['regulador'];
$principio = $Numero - $regulador;
$conexion = mysqli_connect($host, $user, $pw, $dataBase1) or die ("no se ha podido conectar");
$result = mysqli_query($conexion, "SELECT * FROM Noticia1 ORDER BY 'ID' DESC LIMIT {$principio},10");
while($final = mysqli_fetch_array($result)){
echo "
<div class='mensaje'>
<div class=\"not7\"><div class=\"fotosMain3\" id=\"foto18\"><img src='".$final['RutaImagen']."' class='PORTADAS1'></div><div class=\"TextoDerecha\"><h3 id=\"Etiqueta16\" style=\"color: #10A2E5; font-family: 'Raleway', sans-serif; font-size: 12px; line-height: 10px; margin-top: 10px; margin-bottom: 10px;\">".$final['Etiqueta']."</h3><hr class=\"barrasNuevas\" size=\"1px\" width=\"100%\" noshade=\"noshade\" style=\"margin: 10px auto; opacity: 0.3;\"><h1 class=\"titulosMain3\" id=\"Titulo19\">".$final['Titulo']."</h1><h1 class=\"Subtitulos\" id=\"Subtitulo15\">".$final['Subtitulo']."</h1><p style=\"color: #2E2E2E; font-family: Arial; font-size: 11px; margin-top: 10px;\">Por: <span style=\"color: #1279a8;\">".$final['Autor']."<span style=\"color: #2E2E2E;\">|</span></span> <span>".$final['Fecha']."</span></p><p class=\"main3Textos\" id=\"main3Textos-12\">".substr($final['Texto'], 0, 300)."...</p></div></div>
? >
This code receives the variables $ Number and $ regulator, marking the limits in the sql query. For example: I have 30 rows on my table, so:
$ Number = 30
$ beginning = $ Number - $ regulator
$ beginning = 30 - (var number - var page)
$ beginning = 30 - (30 - 7)
$ beginning = 30 - 23
$ beginning = 7
Therefore, the beginning of my DESCENDING limit will be 8 (as counted from 0, since 7 will be row 8) and will bring 10 rows, which logically should be 22 to 13 (10 rows in form descending)
However, the code does not do it that way. It brings me rows up and mixed ...!
Could someone help me ?? Please