limit links in php posgresql paging

Asked

Viewed 40 times

-1

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

The intended:

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>';
}
  • Related: https://answall.com/questions/76906/links-para-pagina%C3%A7%C3%A3o-com-limites-de-exibi%C3%A7%C3%A3o-dos-links

1 answer

0


Solved in 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>";
            }
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.