Mark current page number (PHP paging)

Asked

Viewed 179 times

0

I made a pagination for my page, however, I do not know how I can make the number of the current page to be highlighted and darkened. Could you help me?

Just follow my code:

<?php
//PAGINAÇAO
if ($registros_ > 3) {
//Verificar a pagina anterior e posterior


    $max_links = 6;

    $links_laterais = ceil($max_links / 2);

    $pagina_anterior = $pagina - $links_laterais;
    $pagina_posterior = $pagina + $links_laterais;

    print "<div class='container'>
<div>
                <ul class='pagination'>
                    <li>";
    if ($pagina_anterior > 0) {
        print "<a href='pesquisas.php?pagina=$pagina_anterior&tipo=$tipo&seletor=$seletor&filtro=$filtro' aria-label='Previous'>
                                <span aria-hidden='true'>&laquo;</span>
                            </a>";
    } else {
        print "<span aria-hidden='true'>&laquo;</span>";
    }
    print "</li>";
//Apresentar a paginacao

    for ($i = $pagina_anterior; $i < $pagina_posterior; $i++) {
        if (($i >=1) && ($i <= $num_pagina)){ 
        print"<li><a href='pesquisas.php?pagina=$i&tipo=$tipo&seletor=$seletor&filtro=$filtro'>$i</a></li>";
    }
    }

    print "<li>";
    if ($pagina_posterior <= $num_pagina) {
        print " <a href='pesquisas.php?pagina=$pagina_posterior&tipo=$tipo&seletor=$seletor&filtro=$filtro' aria-label='Previous'>
            <span aria-hidden='true''>&raquo;</span>
        </a>";
    } else {
        print "<span aria-hidden='true'>&raquo;</span>";
        }
    print "</div></div>";
}
//FIM PAGINAÇAO
?>

I was able to solve it this way:

<?php
//PAGINAÇAO
if ($registros_ > 3) {
//Verificar a pagina anterior e posterior


    $max_links = 6;

    $links_laterais = ceil($max_links / 2);

    $pagina_anterior = $pagina - $links_laterais;
    $pagina_posterior = $pagina + $links_laterais;

    print "<div class='container'>
<div>
                <ul class='pagination'>
                    <li>";
    if ($pagina_anterior > 0) {
        print "<a href='pesquisas.php?pagina=$pagina_anterior&tipo=$tipo&seletor=$seletor&filtro=$filtro' aria-label='Previous'>
                                <span aria-hidden='true'>&laquo;</span>
                            </a>";
    } else {
        print "<span aria-hidden='true'>&laquo;</span>";
    }
    print "</li>";
//Apresentar a paginacao

//Criei essa variavel com o código CSS
$numativo = "style='background-color: #4682B4; color: white;'";
//Criei essa variavel com o código CSS

    for ($i = $pagina_anterior; $i < $pagina_posterior; $i++) {
        if (($i >=1) && ($i <= $num_pagina)){ 
        //INCLUINDO ESSE TRECHO
        $ativo = ($i == $pagina) ? $numativo : '';
        //INCLUINDO ESSE TRECHO
        print"<li><a href='pesquisas.php?pagina=$i&tipo=$tipo&seletor=$seletor&filtro=$filtro'
<!--Adicionei a variavel aqui para saber qual a numeração está ativa no momento-->
        $ativo
<!--Adicionei a variavel aqui para saber qual a numeração está ativa no momento-->
        >$i</a></li>";
    }
    }

    print "<li>";
    if ($pagina_posterior <= $num_pagina) {
        print " <a href='pesquisas.php?pagina=$pagina_posterior&tipo=$tipo&seletor=$seletor&filtro=$filtro' aria-label='Previous'>
            <span aria-hidden='true''>&raquo;</span>
        </a>";
    } else {
        print "<span aria-hidden='true'>&raquo;</span>";
        }
    print "</div></div>";
}
//FIM PAGINAÇAO
?>

  • In the https://answall.com/q/26303/3635. answer you have an example of how to show which is the current page and from there you customize as you wish.

1 answer

3

Just compare to the current page:

if (($i >=1) && ($i <= $num_pagina)){
   if ($i == $pagina) {
      // se é a atual, nem do link precisa (conforme o caso)
      print"<li><strong>$i</strong></li>";
   } else {
      // se é diferente da atual, usa link e estiliza diferentemente
      print"<li><a href='pesquisas.php?pagina=$i&tipo=$tipo&seletor=$seletor&filtro=$filtro'>$i</a></li>";
   }
}

Of course you can simplify, the important thing is the comparison of $i with $pagina:

if (($i >=1) && ($i <= $num_pagina)){
   print "<li".($i==$pagina?' class="destacado"':'')."><a href='pesquisas.php?pagina=$i&tipo=$tipo&seletor=$seletor&filtro=$filtro'>$i</a></li>";
}
  • Thank you very much for the friendly reply, but in that way it did not work.

  • Opa, I checked now and saw that was careless, I did not realize, but , it worked yes, however, the number is not highlighted, it moves and is out of the arrangement of the numbers in linlk.

  • Well, then you have to format the CSS, etc. Note that it was just an example, if you need can use with link even to keep the style

  • In the second option, it uses a "class = highlighted" but you have to put in the CSS this class, as the rest of the page

  • Nothing prevents you from repeating the top code with link and everything and only the "Strong" too. The important thing is that you understand that you can put the code you want in each part of the IF.

  • An example would be to use the 2nd code (the simplified one) and in the CSS add .destacado {background-color:#900;text-weight:bold} - this class would be applied only in the numbering of the current page

Show 1 more comment

Browser other questions tagged

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