limiting links in paging php posgresql

-1

I have this code that makes me paging, but I intend to limit the 4 links to each side.

Wanted:

Previous , 11 , 12 , 13 , 14 , 15, 16 , 17 , 18 , 19 , Next

Current code:

$qry1= pg_query($dbconn,"select * from tabela");

$registros = 100;

if(isset($_GET["pag"])){ $pag= $_GET["pag"]; }else{ $pag= 1; }

if($registros > pg_num_rows($qry1)){ $pag= 1; }

$inicio = ($pag- 1) * $registros; 

$qry2= pg_query($dbconn,"select * from tabela limit $registros offset $inicio");

$totalregistros = pg_num_rows($qry2);

$totalpag = ceil(pg_num_rows($qry1) / $registros);


for ($i=1; $i<=$totalpag; $i++){
    echo '<ul class="paginacao"><li $active><a href="index.php?pag='.$i.'">'.$i.'</a>&nbsp;</li></ul>';
}
    
asked by anonymous 13.08.2018 / 17:09

1 answer

0

Solved this way:

    $dottedBefore = false;
    $dottedAfter = false;

    if ($totalpag > 1 && $pag<= $totalpag) {
        for ($i = 1; $i <= $totalpag; $i++) {
            if ($i == $pag) {
                echo "<i>[" . $i . "]</i>";
            } else if ($i < ($pag- 5) && $i != 1) {
                if (!$dottedBefore) {
                    echo "<i>[...]</i>";
                    $dottedBefore = true;
                }
            } else if ($i > ($pag+ 5) && $i < $totalpag) {
                if (!$dottedAfter) {
                    echo "<i>[...]</i>";
                    $dottedAfter = true;
                }
            } else {
                echo "<i><a href='index.php?pag=" . $i . "'>[" . $i . "]</a></i>";
            }
        }
    }
    
13.08.2018 / 18:38