Wrong current page tag in Bootstrap4 pagination

Asked

Viewed 16 times

0

I am trying to use bootstrap4 pagination but the active page is coming wrong. For example, the first page comes as page 2. My code looks like this:

            <nav>
              <ul class="pagination">
                <li>
                  <a href="?id=45&pg=<?php echo $num_paginas-1; ?>" aria-label="Anterior">
                    <span aria-hidden="true">&laquo;</span>
                  </a>
                </li>
                <?php 
                for($i=0;$i<$num_paginas;$i++){
                $estilo = "";
                if($pagina == $i)
                    $estilo = "class=\"active\"";
                ?>
                <li <?php echo $estilo; ?> ><a href="?id=45&pg=<?php echo $i; ?>"><?php echo $i+1; ?></a></li>
                <?php } ?>
                <li>
                  <a href="?id=45&pg=<?php echo $num_paginas+1; ?>" aria-label="Próximo">
                    <span aria-hidden="true">&raquo;</span>
                  </a>
                </li>
              </ul>
            </nav>          

1 answer

0


From what I understand the variable $page is being captured via GET as the current page, right?

Assuming the get is being passed as ? id=45&pg=1, to page 1, in which case when $i is 1 it will enter if

if($pagina == $i)
                    $estilo = "class=\"active\"";

and will put the style to being active page, however, at the time of showing, $i is getting +1, so $i+1 = 2.

Or you can display $i without adding the value or counting 0 as page 1.

Browser other questions tagged

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