Paging using PHP

Asked

Viewed 72 times

0

I made a pagination using PHP that is working perfectly, however, when arriving at the penultimate page(10), the last page(11) it comes as a result "loose" and does not come stylized as if it were a "bug" Follows the image with the error:

inserir a descrição da imagem aqui

Follows the code:

<ul class="pagination">

                <?php
            $pagina_atual = (!isset($_GET['p']))? 1 : $_GET['p']; 
            $max_results = 1;
            $prev = ($pagina_atual - 1);
            $next = ($pagina_atual + 1);
            $total_pages = ceil($paginas / $max_results);
            $pagination = '';
            if($paginas > 1) {
                $pagination .= '<li class="pagination_reborn"><a class="page " href="logacesso?p='.$prev.'">«</a></li> ';
            }

            for($i = max(1, $pagina_atual - 4); $i <= min($pagina_atual + 4, $total_pages); $i++) {
                if(($paginas) == $i)
                {
                    $pagination .= $i;

                }
                else
                {
                if($pagina_atual == $i) {
                        $pagination .='<li id="active_page" class="pagination_reborn"><a class="page" href="logacesso?p='.$i.'">'.$i.'</a></li>';
                } else {
                        $pagination .= '<li class="pagination_reborn"><a class="page" href="logacesso?p='.$i.'">'.$i.'</a></li>';
                    }
                }
            }
            if($pagina_atual < $total_pages)
            {
                $pagination .= '<li class="pagination_reborn"><a href="logacesso?p='.$next.'">»</a></li>';
            } 
            if($next == $i) {
                $pagination .= '<li class="pagination_reborn"><a class="page" href="logacesso?p='.$i.'">'.$i.'</a></li>';
            }


            echo $pagination;

            ?>
  • Try enumerating For from scratch, since 0 is an index.

1 answer

1

You should start enumerating your pages from scratch(Adding +1 to the display).

if to display the button on the previous page you need one more rule not to display when on the first page.

The first if from inside the for is not necessary, since you only have two cases, active and non-active page.

The last if is not necessary as it will never be called.

$pagina_atual = (!isset($_GET['p'])) ? 1 : $_GET['p'];
$max_results = 1;
$prev = ($pagina_atual - 1);
$next = ($pagina_atual + 1);
$total_pages = ceil($paginas / $max_results);
$pagination = '';

if ($paginas > 1 && $pagina_atual > 1) {
    $pagination .= '<li class="pagination_reborn"><a class="page " href="logacesso?p=' . $prev . '">«</a></li> ';
}

for ($i = max(0, $pagina_atual - 4); $i < min($pagina_atual + 4, $total_pages); $i++) {
    $page = $i + 1;

    if ($pagina_atual == $page) {
        $pagination .= '<li id="active_page" class="pagination_reborn"><a class="page" href="logacesso?p=' . $page . '">' . $page . ' ativo</a></li>';
    } else {
        $pagination .= '<li class="pagination_reborn"><a class="page" href="logacesso?p=' . $page . '">' . $page . '</a></li>';
    }
}
if ($pagina_atual < $total_pages) {
    $pagination .= '<li class="pagination_reborn"><a href="logacesso?p=' . $next . '">»</a></li>';
}
  • Continues with same error ;/ Last number appears.

  • @Try again, see if you forgot something, I checked the code line by line and it’s ok

Browser other questions tagged

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