Doubt with page calculation in php pager

1

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>&nbsp;
    <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>
&nbsp;<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.)

    
asked by Killpe 15.12.2017 в 12:59
source

2 answers

3

The problem is this:

Suppose the number of records is 12 and will be listed per page 5 , that is:

$Maxpagina = 12; // NOTA: Considera cambiar el nombre a algo como $TotalRegistros ;)
$Cuantos = 5;

$Maxpagina / $Cuantos = 2.4 // <= con decimales

When using round the decimal part is lost:

round($Maxpagina / $Cuantos) = 2;

But to see ALL the items we should have go through 3 pages, that is:

  • Page 1 : from 1 to 5
  • Page 2 : from 6 to 10
  • Page 3 : from 11 to 12

Solution:

You could use ceil that:

  

Round fractions up

$PagMax = ceil($Maxpagina / $Cuantos); // = 3

But as your pages go from 0 ( cero ) onwards, then:

$PagMax = ceil($Maxpagina / $Cuantos) - 1 // = 2;

NOTE: Remember that you must always validate the values sent by GET , POST , etc. to avoid headaches, for example, That $ _GET ["page"] and $ _GET ["how many"] are whole

    
answered by 15.12.2017 / 13:40
source
1

The error is as follows:

$PagMax = round($Maxpagina / $Cuantos -1);

Replace it with:

$PagMax = round($Maxpagina / ($Cuantos -1));

You are subtracting 1 from the number of pages. No to the number of records.

    
answered by 15.12.2017 в 13:12