I have made a pager by hand to filter the number of results of a query to mysql that, when it had many records, took too long.
This is the code I have 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 I pass to this function is:
$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 no longer had results, it would take out the blank page, so I have put a page counter to know how many pages there are and in the case of reaching the last The next page button no longer works. But I don't know why something tells me that I'm not doing the calculation right.
I get that calculation in the 2nd and 3rd line of code:
$PagMax = round($Maxpagina / $Cuantos -1);
if ($Pagina == $PagMax) { $UltimaPagina = 0; } else { $UltimaPagina = 1;}
Basically what I am doing is: I round off the operation resulting from dividing the number of records by the amount per page and I Subtract 1 (I subtract that one because apparently in mysql record 1 is equal to 0) and if the page number in the one I am is equal to the resulting rounding operation, I cancel the next page button.
The thing 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 (which starts at 0) and the number of records I want to display.
at the end of the query after the ORDER BY I put this:
LIMIT $STPagina, ".mysqli_real_escape_string($Conectar, $_GET["Cuantos"])."
I have the same error here.
The total number of records is taken without applying the limit (that is, to all the records that exist in the table.)
The problem is the following:
Suppose that the number of records is
12
y will be listed per page5
, that is:When using
round
the decimal part is lost:But to see ALL the items we should have to traverse
3
pages, that is:Solution:
You could use
ceil
that:But since your pages go from 0 (
cero
) onwards, then:NOTE: Remember that you must always validate the values sent by
GET
,POST
, etc. to save you headaches, for example, Make $_GET["page"] and $_GET["How many"] be integersThe error is the following:
Replace it with:
You are subtracting 1 from the number of pages. No to the number of records.