Improving paging scheme - PHP and Javascript

1

In the site that I took on a client, some pages have a paging scheme. For example, a news page showing a record of 100 news stories appears 7 news stories per page. The pagination will show me from 1 to 15. Here is the pre-defined paging code:

<?php if(@$data['quantidade_paginas'] > 1){;?>
<div class="blocoPaginacao">
<div class="paginacao">
    <span pg="anterior" class="btnSetaVoltar" style="opacity: 0"></span>
    <span pg="anterior" class="linkControle font12" style="opacity: 0"> ANTERIOR </span>
    <ul class="btsPaginacao">
        <?php for($i=1; $i<=$data['quantidade_paginas']; $i++){ ?>
        <li>
            <span pg="<?php echo $i?>" pagina_="<?php echo $i?>" class="btnPaginar<?php echo ($i==1 ? ' Sel' : ''); ?>"> <?php echo $i?> </span>
        </li>
        <?php } ?>
    </ul>
    <span pg="proxima" class="linkControle font12"> PRÓXIMA </span>
    <span pg="proxima" class="btnSetaAvancar"></span> 
</div>
<input type="hidden" id="total_paginas" value="<?php echo $i-1?>">
<input type="hidden" id="pagina_atual" value="1">
</div>
<?php } ?>

The problem is if I have about 50 pages. The paging scheme will show me 50 page numbers so I can choose. I wanted it to appear only from 1 to 10, for example. And clicking from 10, for example, start appearing from 11 to 19 and so on.

What will it be like?

    
asked by anonymous 04.11.2016 / 12:54

2 answers

1

You will need to create 2 new variables and do the following calculations.

  • 1st Variable: Página * Quantidade_de_Página
  • 2nd Variable: (Página * Quantidade_de_Página) - Quantidade_de_Página + 1;

There you will get the results you want. Example: Page 1 from 1 to 10

    
04.11.2016 / 14:24
0

First of all, you need to know the current page:

$currentPage = 2;

Next, set the number of pages you want to display:

$displayedPages = 10;

Next, you should find the "middle ground" (it would be easier if the number of pages displayed was odd, but "whatever"):

$middlePage = ceil($displayedPages / 2);//5 (caso for número impar, arredondará para baixo)

You should set which number to start, because negative pagination can not start:

$startDisplayedPage = $currentPage <= $middlePage ? 1 : $currentPage  - $middlePage + 1;

If the current page is less than half the pages to be displayed (in this case it is 5), it starts at number 1. If not, it is half + 1 (if the current page is 10, start count 6 and the fifth number is the current page).

However, there is the possibility of being a page near the end of the listing, in which case you also need to validate:

 $total = 70; //total de páginas

And then validate:

 $startDisplayedPage = $startDisplayedPage >= $total - $displayedPages ? ($total - $displayedPages + 1) : $startDisplayedPage;

If the home page is larger than the total minus the pages to display (which in the calculation will be 60, you must add 60 + 1, to start with 61). Otherwise, start as previously calculated.

Now go to the creation of the links:

for($i = 0; $i < $displayedPages ; $i++)
{
    $values[] = sprintf(
        '<a href="?page=%1$d" title="Página %1$d">%1$d</a>',
        $startDisplayedPage++ //adiciona a página corrente e, após, incrementa 1
    );
}

echo implode(' | ' , $values);

Values have been added to the array for print / preview issues only. But it will ease your process.

And the output:

  

1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10

If, by chance, the current page is 50:

  

46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55

And if, on the other hand, the number of pages to be displayed was 11 (odd):

  

45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55

If the current page is 65:

  

61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70

    
04.11.2016 / 18:26