Improving Paging Scheme - PHP and Javascript

Asked

Viewed 144 times

1

On the site I took over from a client, some pages have a pagination scheme. For example, a news page that shows record of 100 published news appear 7 news per page. The pagination will show me from 1 to 15. Here is the predefined code of the pagination:

<?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 50 pages. The paging scheme will show me 50 page numbers so I can choose. I wanted it to only appear from 1 to 10, for example. And clicking from 10, for example, start to appear from 11 to 19 and so on.

Have it be?

2 answers

1

You will need to create 2 new variareis and make 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 as you want. Ex.: Pag 1 of 1 to 10

  • I don’t think it’ll do. For example, if I have 20 pages and I am on page 9: variable1 = 20*9 = 180 and variable2 = 180-20+1 = 161; why would I use these large variables?

0

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

$currentPage = 2;

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

$displayedPages = 10;

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

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

You must define the number you will start on, as you cannot start negative actions:

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

If the current page is less than half the pages to be displayed (in case it is 5), it starts at number 1. Otherwise, it is half + 1 (if the current page is 10, starts counting 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 homepage is larger than the total minus the pages to be shown (which in the calculation will be 60, you should add 60 + 1, starting at 61). Otherwise, start as previously calculated.

Now it’s time to start creating 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 only for print/preview issues. But it will make your process easier.

And the exit:

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

If by any chance the current page were 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

Browser other questions tagged

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