How can I limit the page numbers shown in the pagination? - php

I am trying to limit the shown pagination. My site has 500+ pages, and all 500+ numbers are shown in the pagination.
I am trying to limit it like this:
Prev 1 2 3 4 5 6 Next
My code:
$skin = new skin('site/pagination'); $pagination = '';
if ($pages >= 1 && $page <= $pages) {
for ($x=1; $x<=$pages; $x++) {
$TMPL['pagination'] = ($x == $page) ? '<strong>'.$x.'</strong> ' : ''.$x.' ';
$pagination .= $skin->make();
}
}

pagination page number limit problem solve by chnage
for ($x=1; $x<=$pages; $x++)
to
for($x = max(1, $page - 5); $x <= min($page + 5, $pages); $x++)

What do you expect this to do?:
for ($x=1; $x<=$pages; $x++)
It is going to create an entry for every page. If you don't want that, limit it how it makes sense:
for ($x=1; $x <= min(5, $pages); $x++)
Even better would be to consider the current page:
for ($x=max($curpage-5, 1); $x<=max(1, min($pages,$curpage+5)); $x++)

//Let's say you want 3 pages on either side of your current page:
$skin = new skin('site/pagination'); $pagination = '';
$currentPage = get the current page number however you have it stored;
// set the lower bound as 3 from the current page
$fromPage = $currentPage - 3;
// bounds check that you're not calling for 0 or negative number pages
if($fromPage < 1) {
$fromPage = 1;
}
// set the upper bound for what you want
$toPage = $fromPage + 7; // 7 is how many pages you'd like shown
// check that it doesn't exceed the maximum number of pages you have
if($toPage > $maxPages) {
$toPage = $maxPages;
}
// iterate over your range
for ($x=$fromPage; $x<=$toPage; $x++) {
$TMPL['pagination'] = ($x == $page) ? '<strong>'.$x.'</strong> ' : ''.$x.' ';
$pagination .= $skin->make();
}

For large numbers of pages, consider displaying links using "logarithmic" pagination. See my answer here (PHP code included):
How to do page navigation for many, many pages? Logarithmic page navigation

I tried the answers provided by wallyk and Hemang but they fell short for my pagination case. Their answers would sometimes display less links than the range. I had to add some max and min statements.
Here is my take in Javascript code:
var minPage = Math.max(Math.min(currentPage - (range / 2), totalPages - range), 0);
var maxPage = Math.min(Math.max(currentPage + (range / 2), range), totalPages);
The range is the number of links always displayed.
The totalPages is the total number of pages to iterate over.
The currentPage is the currently displayed page.
Note that my pagination index is 0 based.

Related

Get numbers next and after a certain number with dynamic max results with PHP

I have a pagination which renders some pages. I have a setting where the user can define how many pages are to be displayed as numbers in the pagination nav. The first and last number are always visible. Now if the user wants to have 3 numbers on the nav and assuming that i am now in the page 7, then it should look like this:
1 ... 6 7 8 ... 12
If the user wants four items then it should look like that:
1 ... 6 7 8 9 ... 12
Up until now i have the following which gives me 3 before and after the current page
$maxLinks = 3;
$currentPageNumber = 7;
$pages = [];
$pages[] = 1;
for($i = max(2, $currentPageNumber - $maxLinks); $i <= min($currentPageNumber + $maxLinks, 12 - 1); $i++) {
$pages[] = $i;
}
$pages[] = 12;
foreach ($pages as $key => $page) {
$newPage = '...';
if (($key === 0) && $pages[1] !== $page + 1) {
array_splice( $pages, 1, 0, $newPage );
}
$itemBeforeLast = count($pages)-2;
if (is_numeric($pages[$itemBeforeLast]) && ($key === $itemBeforeLast) && $pages[$itemBeforeLast + 1] !== $pages[$itemBeforeLast] + 1) {
array_splice( $pages, $itemBeforeLast +1, 0, $newPage );
}
}
This gives me back the following:
But i only want to get 3 or 4 numbers between the dots (this changes based on the value that the user gives in the settings ($maxLinks variable))
Any help is deeply appreciated
Best regards
As you add them to both sides, you need to divide by 2. Also, remove 1 to account for the current page. Then you just need to account for the possibility of having a non-even number of links to the left&right by rounding (down for the left, up for the right).
And end up with:
for($i = max(2, $currentPageNumber - floor(($maxLinks-1)/2));
$i <= min($currentPageNumber + ceil(($maxLinks-1)/2), 12 - 1); $i++)

How to build a pagination with ellipsis

so, I read every related questions here but they were not what Im asking here so please if you feel this question is a duplicate consider some one new to PHP asking this who can't find any good answer and I'm also asking please do not down vote this maybe some one wants to help me. Thanks
btw this is my code for pagination:
<?php
$per_page = 10;
if(isset($_GET['page'])){
$page = $_GET['page'];
} else {
$page = 1;
}
if($page == "" || $page == 1) {
$page_1 = 0;
} else {
$page_1 = ($page * $per_page) - $per_page;
}
$item_count = "SELECT * FROM products";
$find_count = mysqli_query($connection, $item_count);
$count = mysqli_num_rows($find_count);
$count_pages = ceil($count / $per_page) ;
?>
<ul class="pagination">
<?php
for($i = 1; $i <= $count_pages; $i++){
if($i == $page) {
echo "<li><a class='active-page' href='./latest.php?page=$i'>$i</a></li>";
} else {
echo "<li><a href='./latest.php?page=$i'>$i</a></li>";
}
}
?>
</ul>
the code get the page number with a Get request and do a loop for number of pages. and pagination out put is like this :
1 2 3 4 5 6 7 8 9 10
but I dont want to display like above, I want to make this shorter with "..." , like this if the user is on page 3:
1 2 3 4 5 ... 9 10
or user is on page 9 :
1 2 ... 7 8 9 10
how can I manipulate this code to achieve this?
Hey there The Big Ash!
You're almost there already. You just need to check a couple of things:
is $i within two pages of the beginning or the end of the page count. That's easy, right?
if ($i <= 2 || $i >= $count_pages - 2)
is $i within two pages of the current page?
This is achieved with
if (abs($i - $page) <= 2)
So now the question remains: when to put the ellipsis?
If you just echo '...' every time the above conditions are not met, you'll just end up with a whole bunch of ellipses, right?
Also, it's possible that you'll need two ellipses (imagine there are 20 pages and you're on page 10. You'd want '1 2 ... 8 9 10 11 12 ... 19 20).
I'm sure there's a more elegant way to this, but I'd just use a flag ($outOfRange) which is set to false when any of the above conditions are met, but set to true when they are not. Then we echo '...' only when the conditions are not met but $outOfRange is still false. So we have:
$outOfRange = false;
for($i = 1; $i <= $count_pages; $i++) {
if ($i <= 2 || $i >= $count_pages - 2 || abs($i - $page) <= 2) {
// page number should be echoed so do as you did before
$outOfRange = false;
if($i == $page) {
echo "<li><a class='active-page' href='./latest.php?page=$i'>$i</a></li>";
} else {
echo "<li><a href='./latest.php?page=$i'>$i</a></li>";
}
} else {
// we are out of range! if not already out of range, echo ellipsis
if (!$outOfRange) {
echo ' ... ';
}
$outOfRange = true;
}
}

Can not send levels to switch pages

Am working on array pagination. I am having some trouble forming equations. what I want is when
$CurrentPage = 1 then $Start = 1,
if $CurrentPage = 2 then $Start = 30,
if $CurrentPage = 3 then $Start = 60,
if $CurrentPage = 4 then$Start = 90
and so on..
How do I write the if else block ?
you can also use this:
$Start = ($currentPage==1 ? 1 : ($currentPage-1)*30);
and forget all the if/else.
If the current page is one, display from result one (for what ever reason not from result 0), if page bigger than one display from result (page - 1)*30
if ((int)$currentPage > 1) {
$start = ($currentPage - 1)*30;
}
else {
$start = 1;
}
or in a shorter way
$start = ($currentPage > 1) ? ((int)$currentPage - 1) * 30 : 1;
Keep it simple:
$Start = max(1, ($currentPage-1) * 30);

How set paging in PHP?

I have an 300 rows(data) each page contain 10 records I need first 10 pages then click next then display next 10 page here is my code:
<?php
$eu = ($start - 0);
$limit = 10;
$this1 = $eu + $limit;
$back = $eu - $limit;
$next = $eu + $limit;
if ($nume > $limit)
{
echo'<div class="pagination right">';
if ($back >= 0)
{
echo"<a href='$page_name?start=$back&o" . $_SESSION['clicked'] . "&p=$desc'>«</a>";
}
$i = 0;
$l = 1;
for ($i = 0; $i < $nume; $i = $i + $limit)
{
if ($i <> $eu)
{
echo"<a href='$page_name?start=$i&o=" . $_SESSION['clicked'] . "&p=$desc'>$l</a>";
}
else
{
echo "<a class='active'>$l</a>";
}
$l = $l + 1;
}
if ($this1 < $nume)
{
echo "<a href='$page_name?start=$next&o=" . $_SESSION['clicked'] . "&p=$desc'>»</a>";
}
echo '</div>';
}
echo '</div></div>';
?>
Then I will get response like this http://app.ologie.us/app/admin/Screen.png
Can any one please guide to effective paging in PHP.
Here's a tutorial on basic pagination.
http://www.phpfreaks.com/tutorial/basic-pagination
Doing it like Tim said is, according to me, mainly a bad way to do this. Read in the data needed WHEN it's needed.
You could do it with a min - max index to your array.
Consider you want to display 10 records per page:
$min = $page * 10; //Page is the current page
$max = $min + 10;
Then you just loop through the data between the min - max indexes.
for($i = $min; $i < $max; $i++) {
//Echo your data like normal
}
If you're gathering your data from MySQL, you might want to look up LIMIT.
If $limit is your page size and you add another request variable $page - representing the search result page requested by the user - you can use
$start = ($page-1)*$limit;
for($i=$start;$i < $start + $limit;$i=$i+1) {
...
}
to iterate over a "page" of the results.
But of course it does not make sense to handle paging on the level of the result view. You want to limit the number of results you get from the query.
In addition I would really consider using a PHP framework rather than coding it in the way you present in your request. Chances are your application will end up to be difficult to maintain.
You do it all client side with the DataTables plugin for jQuery. This plugin also gives you a LOT more options like sorting and searching.
http://datatables.net/
If you have a VERY large table, this probably isn't a good idea, though.

how to show hit counter then make it back to zero?

i want after i've been submit form it can show hit counter..
but i want after it reach "20" it can back to zero..bcoz the limit of submit is 20 times so it can't over the limit.
how do i make it works?I've been try to this code...
<?
$limit="20";
$Query_counter=mysql_query("SELECT model FROM inspec");
$Show_counter=mysql_fetch_array($Query_counter);
$show_counter = $show_counter["model"]+1;
if($show_counter > $limit[0]) {
$show_counter = 0;
}elseif ($show_counter > $limit[1]) {
$show_counter = 0;
}
$Query_update=mysql_query("UPDATE inspec SET model=$Show_counter");
$Show_counter=number_format($Show_counter);
$Show_counter=str_replace(",",".",$Show_counter);
echo "Hit:</br><strong>$show_counter</strong>";
?>
To make a counter go up to a certain value then loop back to zero, you can use the modulus operator, which in a lot of languages (including PHP and MySQL) is %
$x = 0;
$limit = 4;
for ($i = 0; $i < 10; ++$i) {
$x = ++$x % $limit;
echo $x;
}
// 1, 2, 3, 0, 1, 2, 3, 0, 1, 2
I hope that makes enough sense. I can't really figure out from the question what exactly you want... Perhaps something like this?
UPDATE `mytable` SET `mycounter` = (`mycounter` + 1) % {{the limit}}
The concept here is to test to see if the variable you're incrementing exceeds some acceptable range as a result of the next increment. Simple increment the variable, then test its value.
In your case, just add a test after you increment the counter:
$show_counter = $show_counter["model"]+1;
if($show_counter > $limit){
$show_counter = 0;
}
Be sure to define $limit to whatever number you want to cycle on.
If you want to do this for multiple thresholds you can add additional tests. Note that you could hard-code $limit to any number or any variable you want, it's just the thing you're testing against.
<?
$Query_counter=mysql_query("SELECT model FROM inspec");
$Show_counter=mysql_fetch_array($Query_counter);
$show_counter = $show_counter["model"]+1;
$x = 0;
$limit = 20;
for ($i = 0; $i < 30; ++$i) {
$x = ++$x % $limit;
echo $x;
}
$Query_update=mysql_query("UPDATE inspec SET model= (model + 1) % 20");
$Show_counter=number_format($Show_counter);
$Show_counter=str_replace(",",".",$Show_counter);
echo "Hit:</br><strong>$show_counter</strong>";
?>

Categories