I have made a pager by hand to filter the amount of results of a query to mysql that when it had many records took too much time.
this is the code that I used:
function Paginador($Que,$Pagina,$Cuantos,$Maxpagina,$IdOrden) {
$PagMax = round($Maxpagina / $Cuantos -1);
if ($Pagina == $PagMax) { $UltimaPagina = 0; } else { $UltimaPagina = 1;}
$Paginador = '<div class="Paginador">
<i class="fas fa-angle-left"';
if ($Pagina != 0) {
$Paginador .='onclick="$(\'#'.$Que.'Pagina\').val(Number($(\'#'.$Que.'Pagina\').val()) - 1).change()" ';
} else {
$Paginador .='style="color: #CCC;"\';'; } $Paginador .='></i>
<select class="CuanPaginador" onchange="$(\'#'.$Que.'Cuantos\').val(this.value).change(); $(\'#'.$Que.'Pagina\').val(0)">
<option '; if ($Cuantos == 5) { $Paginador .= "selected"; } $Paginador .=' value="5">5</option>
<option '; if ($Cuantos == 50) { $Paginador .= "selected"; } $Paginador .=' value="50">50</option>
<option '; if ($Cuantos == 100) { $Paginador .= "selected"; } $Paginador .=' value="100">100</option>
<option '; if ($Cuantos == 500) { $Paginador .= "selected"; } $Paginador .=' value="500">500</option>
</select>
<i class="fas fa-angle-right" ';
if ($UltimaPagina >= 1) { $Paginador .=' onclick="$(\'#'.$Que.'Pagina\').val(Number($(\'#'.$Que.'Pagina\').val()) + 1).change()"';
} else { $Paginador .='style="color: #CCC;"\'; ';
} $Paginador .='></i>
</div>
<script>Ordenable(\''.$IdOrden.'\');</script>';
return $Paginador;
}
The data that happened to this function are:
$Que paso el nombre variable de los input ( para paginar )
$Pagina paso el numero de pagina en la que estoy
$Cuantos paso la cantidad de registros por pagina que quiero
$Maxpagina paso el numero total de registros que hay en la consulta
$Idorden es para activar una función de javascript que no tiene que ver con la paginación.
I have had problems with the last page, because if I went to a page that did not have any results, I would remove the blank page, with which I have put a page counter to know how many pages there are and in the case of arriving to the last one that does not work anymore the next page button. But I do not know why something tells me that I'm not doing the calculation right.
that calculation I take it out in the 2nd and 3rd lines of code:
$PagMax = round($Maxpagina / $Cuantos -1);
if ($Pagina == $PagMax) { $UltimaPagina = 0; } else { $UltimaPagina = 1;}
What I am basically doing is: rounding the resulting operation of dividing the number of records by the amount per page and the rest 1 (the rest of that one because apparently in mysql the record 1 equals 0) and if the number of page in which I am is equal to the resulting operation of rounding, I annul the button of next page.
The fact is that I get the feeling that I'm missing records when I get to the last page.
Would the operation to calculate the page be that?
To calculate the mysql limit I do it like this:
$STPagina= $_GET["pagina"] * $_GET["Cuantos"]; //calculo el registro por el que empezar el paginador
Here I am passing the page number (starting at 0) and the number of records I want to show.
at the end of the query after the ORDER BY I put this:
LIMIT $STPagina, ".mysqli_real_escape_string($Conectar, $_GET["Cuantos"])."
The same thing is the fault here.
The total number of records is removed without applying the limit (b) to all the records that exist in the table.)