I have an pagination code:
//pagination
$result1=$mysqli->query("SELECT * FROM product");
$row_per_page=10 ;
$rows=$result1->num_rows;
if ($rows>$row_per_page) $page=ceil($rows/$row_per_page);
else $page=1;
if(isset($_GET['start']) && (int)$_GET['start'])
$start=$_GET['start'];
else
$start=0;
$result=$mysqli->query("SELECT * FROM product limit $start,$row_per_page");
//End pagination
while ($rows = $result->fetch_assoc()) {
echo $rows['name'];
}
$page_cr=($start/$row_per_page)+1;
for($i=1;$i<=$page;$i++)
{
if ($page_cr!=$i) echo "<div class='pagination'>"."<a href='index.php?go=product&start=".$row_per_page*($i-1)."'>$i </a>"."</div>";
else echo "<div class='pagination'>".$i." "."</div>";
}
This code create pagination like this
Yeah, i want to remove 6 7 8 9 ect... and replace with "...". When i click to page 6, it will remove 1 2 3 4 5 - 10 11 12 13 etc..,it only show 6 7 8 9 as image below
[![Pagination][2]][2]
I hope you can understand my ideal, i try the best to show you.
Thank you so much
Here's a solution to your pagination problem, the kind of solution Google uses to paginate it's search results. Basically, the idea behind this solution is this:
At any point of time, at most 5 pagination links will be displayed i.e. from the current page's perspective, display two predecessor and successor pages. Let me explain this thing using two scenarios,
Case(1): When the number of pages is equal to or less than 5 (Lets say we have 4 pages)
In this case, we have to display all pagination links without any preceding or succeeding dots, like this:
// User is on page 1
1 2 3 4
- - -
// User is on page 3
1 2 3 4
- - -
// User is on page 4
1 2 3 4
- - -
Case(2): When the number of pages is greater than 5 (Lets say we have 10 pages)
In this case, we have to display both the pagination links and dots accordingly, like this:
// User is on page 1
1 2 3 4 5 ...
- - - -
// User is on page 5
... 3 4 5 6 7 ...
- - - -
// User is on page 10
... 6 7 8 9 10
- - - -
So after getting the current page number using $page_cr=($start/$row_per_page)+1;, the algorithm of this custom pagination system would be like this:
Check whether the number of pages is greater than 5 or not. If it's greater than 5, go to step 2 otherwise go to step 5.
Find the superset range of pages, like 1-10, or 1-20 etc. For example, if $page = 10 then this superset range would be 1-10. The code for this step is this:
// Superset range of pages
$superset_range = range(1, $page);
Find the subset range of pages to display, like 1-5, or 3-7 etc. For example, if $page = 10 then this subset range would be 1-5, or 3-7, or 6-10 etc., it can be any consecutive five page between 1 and 10. Also, adjust this range whenever necessary. The code for this step is this:
// Subset range of pages to display
$subset_range = range($page_cr - 2, $page_cr + 2);
// Adjust the range
foreach($subset_range as $p){
if($p <= 0){
array_shift($subset_range);
$subset_range[] = $subset_range[count($subset_range) - 1] + 1;
}elseif($p > $page){
array_pop($subset_range);
array_unshift($subset_range, $subset_range[0] - 1);
}
}
Display the pagination links and dots accordingly. The code for this step is this:
// Display pagination links and dots
if($subset_range[0] > $superset_range[0]){
echo "<div class='pagination'>... </div>";
}
foreach($subset_range as $p){
if($page_cr != $p){
echo "<div class='pagination'>"."<a href='index.php?go=product&start=".$row_per_page*($p-1)."'>$p </a>"."</div>";
}else{
echo "<div class='pagination'>".$p." "."</div>";
}
}
if($subset_range[count($subset_range) - 1] < $superset_range[count($superset_range) - 1]){
echo "<div class='pagination'> ...</div>";
}
Display all pagination links using your old code, like this:
// Display all page links
for($i = 1; $i <= $page; $i++){
if($page_cr != $i){
echo "<div class='pagination'>"."<a href='index.php?go=product&start=".$row_per_page*($i-1)."'>$i </a>"."</div>";
}else{
echo "<div class='pagination'>".$i." "."</div>";
}
}
So here's the complete code:
$result1 = $mysqli->query("SELECT * FROM product");
$row_per_page= 10;
$rows=$result1->num_rows;
if ($rows > $row_per_page){
$page=ceil($rows/$row_per_page);
}else{
$page=1;
}
if(isset($_GET['start']) && (int)$_GET['start']){
$start=$_GET['start'];
}else{
$start=0;
}
$result=$mysqli->query("SELECT * FROM product limit $start,$row_per_page");
while ($rows = $result->fetch_assoc()) {
echo $rows['name'];
}
$page_cr=($start / $row_per_page) + 1; // Page number
if($page > 5){
// From the current page's perspective, display two predecessor and successor pages
// Superset range of pages
$superset_range = range(1, $page);
// Subset range of pages to display
$subset_range = range($page_cr - 2, $page_cr + 2);
// Adjust the range
foreach($subset_range as $p){
if($p <= 0){
array_shift($subset_range);
$subset_range[] = $subset_range[count($subset_range) - 1] + 1;
}elseif($p > $page){
array_pop($subset_range);
array_unshift($subset_range, $subset_range[0] - 1);
}
}
// Display pagination links and dots
if($subset_range[0] > $superset_range[0]){
echo "<div class='pagination'>... </div>";
}
foreach($subset_range as $p){
if($page_cr != $p){
echo "<div class='pagination'>"."<a href='index.php?go=product&start=".$row_per_page*($p-1)."'>$p </a>"."</div>";
}else{
echo "<div class='pagination'>".$p." "."</div>";
}
}
if($subset_range[count($subset_range) - 1] < $superset_range[count($superset_range) - 1]){
echo "<div class='pagination'> ...</div>";
}
}else{
// Display all page links
for($i = 1; $i <= $page; $i++){
if($page_cr != $i){
echo "<div class='pagination'>"."<a href='index.php?go=product&start=".$row_per_page*($i-1)."'>$i </a>"."</div>";
}else{
echo "<div class='pagination'>".$i." "."</div>";
}
}
}
Related
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;
}
}
I'm trying to make a pagination where I can limit how many numbers it will show in the navigation so it will be something like this
< 1 ... [3] 4 5 ... 9 >
The script I have now is working fine until I'm at the last couple of pages. Lets say I set max numbers allowed in the navigation to 4 and number of pages to 9. Then it will work to page 7, but from page 7 it will start shrink in numbers and not put them in front of the current page number. It will behave like this
< 1 ... [7] 8 9
< 1 ... [8] 9
When I'm at the last page the navigation will only show
< 1 ... 9
What i would like it to look like is like this
< 1 ... 6 [7] 8 9 >
< 1 ... 6 7 [8] 9 >
< 1 ... 6 7 8 [9]
I have tried to make this work for some hours now but, are kind of stuck. Any suggestions?
// Method to output the pagination
function output_pagination($max_per_page, $table) {
$page_number = $this->valid_page_number($this->get_page_number(), $table); // Get current page number
$num_pages = $this->count_table_rows($table) / $max_per_page; // Set the number of pages
$max_nav_pages = 4; // Max numbers of pages in the page navigation
$start = $page_number-1; // Value to start the loop on
// Set start to 1 if it equals 0
if($start === 0) {
$start = 1;
}
// Keep loop going until $i no longer is greater or equal to number of pages
for($i = $start; $i <= $num_pages; $i++) {
// Output prev arrow + '...' + 1 if its the start of the pagination and page number not equals 1 ($i = 1 and page number is > $i)
if(($i === $start) && ($page_number > 1)) {
// Check if number of pages are greater then max allowed in navigation and if page number not equals 2
if(($num_pages > $max_nav_pages) && ($page_number !== 2)) {
echo "<< ";
echo "< ";
echo "1 ";
echo " ... ";
$i = $page_number - 1;
} else {
echo "<< ";
echo "< ";
echo $i;
}
}
// Output current page in bold text if $i equals or are greater then $start and page number equals $i
else if (($i >= $start) && ($page_number === $i)) {
echo " <b>[".$i."]</b> ";
}
// Output "...", last page number and next page arrow. if we have 10 pages or more. Then break the loop. We are done.
else if($i === $start + $max_nav_pages) {
// Check if page number equals number of pages - max allowed nav pages + 1
if($page_number === $num_pages-$max_nav_pages+1) {
echo $i;
echo " >";
echo " >>";
break;
} else {
echo " ... ";
echo $num_pages;
echo " >";
echo " >>";
break;
}
}
// Output next page number if $i is greater then $start and $i + 1 is equal or greater then number of total pages
else if(($i > $start) && ($i + 1 <= $num_pages)) {
echo " ".$i." ";
}
else { // Output last page number and next page arrow. We are done.
echo $i;
echo " >";
echo " >>";
}
}
}
Let's say you are in page 8, then the loop always starts from $page_number - 1 which will be 7, which means 6 will never be displayed. You need to start your loop from $page_number - $max_nav_pages and determine which numbers should be displayed that way.
I am fetching username and Id from the database. I have more than 500 usernames. I have to display pagination number only 1 to 5 and last pagination number.
Example:- pagination number is:- 1 2 3 4 5...20(last number).
Now I am getting all numbers in horizontal. Would you help me in this?
Can anyone help me with NEXT and LAST in pagination?
include('../db/connection.php');
$reclimit = 3;
if(isset($_GET['page'])){
$page = $_GET['page'];
} else {
$page = 1;
}
$start = (($page-1) * $reclimit);
$sql = "SELECT * FROM request";
$records =$conn->query($sql);;
$total = $records->num_rows;
$tpages = ceil($total / $reclimit);
$search_sql="SELECT * FROM request LIMIT ".$start."," .$reclimit;
$search_result = $conn->query($search_sql);
HTML
<body>
<?php
if (isset($search_result->num_rows) > 0) {
?>
<h2 class="result-title">Results matching your need</h2>
<?php
while($search_ok = $search_result->fetch_assoc()) {
$user_id=$search_ok['Id'];
$user_name=$search_ok['Name'];
echo "
<div class='search-section'>
<div class='search-profile'>
<div class='s_user_id'>{$user_id}</div>
<div class='s_user_name'>{$user_name}</div>
</div>
</div>
";
}}
for($i=1;$i<=$tpages;$i++) {
echo "".$i."";
}
?>
</body>
The pagination which you are using is simple and working one. But the pagination which you are searching is smart way and you should achieve this by using some if conditions. Similar answer are there in SO. Go to the following, this may help you
Smart pagination algorithm
PHP pagination
Limit pagination page number
It's a simple idea, but I didn't test it. Edit foreach displaying numbers:
$pgStart = 1;
if (isset($_GET['page'])) { // get first showing number = current page - 2
$pg = $_GET['page'] - 2;
$pgStart = $pg + 5 > $tpages ? $tpages - 4 : $pg; //EDIT fix when reach pages end
$pgStart = $pg < 1 ? 1 : $pg; // This must be after ending correction (previous line)
}
if ($pgStart > 1) { // show 1
echo '1 ... ';
}
for($i = $pgStart; $i <= $tpages && $i < $pgStart + 5; $i++) { // show 5 pages
echo ' '.$i.' ';
}
if ($i < $tpages) { // show last
echo ' ... '.$tpages.'';
}
EDIT
Output of this script with $_GET['page'] = 7 and $tpages = 20 from php sandbox is (without linebreaks):
1 ...
5
6
7
8
9
... 20
So i have my pagination links like this.
for ( $counter = 0; $counter <= $page_amount; $counter += 1) {
echo "<a href=\"section.php?q=$section&p=$counter\">";
echo $counter+1;
echo "</a>";
}
And the links grows like:
1 2 3 4 5 6 7 and so on.
But i want to limit this so if there is more than 7 pages it shall only show 7 links like this:
1 2 3 ... 10 11 12
where 12 is the last page.
And if you go to next page it will only change the first pages like this:
3 4 5 ... 10 11 12
until you reach the last 7 pages like this:
6 7 8 9 10 11 12
How do i do this ??
please help.
Here is one way to do it.
// Set some presets
$current_page = 0;
$page_amount = 11;
$limiter = 7;
// Set upper and lower number of links
$sides = round(($limiter/2), 0, PHP_ROUND_HALF_DOWN);
for ( $counter = 0; $counter <= $page_amount; $counter++) {
// Start with Current Page
if($counter >= ($current_page)){
// Show page links of upper and lower
if(($counter <($current_page+$sides))||($counter >($page_amount-$sides))){
echo "<a href=\"section.php?q=$section&p=$counter\">";
echo $counter+1;
echo "</a> ";
}
// The middle link
elseif($counter ==($current_page+$sides)){
echo "<a href=\"page.php?p=$counter\">";
// Show number if number of links == $limiter
if(($page_amount-$current_page)==$limiter-1){
echo $counter+1;
}
// Show '...' number of links > $limiter
else {
echo "...";
}
echo "</a> ";
}
}
}
This allows to change the number of links shown, ie. from 7 to 9.
Note, using PHP_ROUND_HALF_DOWN in round() requires php>=5.3
i'm a newbie in php especially on making pagination.
my question is, how to make paging like stackoverflow's pagination?
i mean paging like this :
1 ... 5 6 7 8 9 ... 25
(the first number and the last number is always appear, but in the middle only 5 numbers with the selected page absolutely in the middle)
in php i have tried making paging,
<?php
//Show page links
for($i=1; $i<=$pages; $i++)
{
echo '<li id="'.$i.'">'.$i.'</li>';
}
?>
but it will be shown all of pages like
1 2 3 4 5 6 7 8 9 10 etc
any body have simple logic example to solve this problem?
many thanks :)
This will generate the numbers as per above with current = 7, pages = 25. Replace the numbers with links to get an actual pagination index.
$current = 7;
$pages = 25;
$links = array();
if ($pages > 3) {
// this specifies the range of pages we want to show in the middle
$min = max($current - 2, 2);
$max = min($current + 2, $pages-1);
// we always show the first page
$links[] = "1";
// we're more than one space away from the beginning, so we need a separator
if ($min > 2) {
$links[] = "...";
}
// generate the middle numbers
for ($i=$min; $i<$max+1; $i++) {
$links[] = "$i";
}
// we're more than one space away from the end, so we need a separator
if ($max < $pages-1) {
$links[] = "...";
}
// we always show the last page
$links[] = "$pages";
} else {
// we must special-case three or less, because the above logic won't work
$links = array("1", "2", "3");
}
echo implode(" ", $links);
Output:
1 ... 5 6 7 8 9 ... 25
Below is a snippet from a general pagination class1 I wrote a few years ago. I have edited it to show the relevant parts only.
// cntAround is the number of pages to show before and after the current
function renderNavigation($cntAround = 1) {
$out = '';
$isGap = false; // A "gap" is the pages to skip
$current = // Current page
$cntPages = // Total number of pages
for ($i = 0; $i < $pages; $i++) { // Run through pages
$isGap = false;
// Are we at a gap?
if ($cntAround >= 0 && $i > 0 && $i < $cntPages - 1 && abs($i - $current) > $cntAround) { // If beyond "cntAround" and not first or last.
$isGap = true;
// Skip to next linked item (or last if we've already run past the current page)
$i = ($i < $current ? $current - $cntAround : $cntPages - 1) - 1;
}
$lnk = ($isGap ? '...' : ($i + 1)); // If gap, write ellipsis, else page number
if ($i != $current && !$isGap) { // Do not link gaps and current
$lnk = '' . $lnk . '';
}
$out .= "\t<li>" . $lnk . "</li>\n"; // Wrap in list items
}
return "<ul>\n" . $out . '</ul>'; // Wrap in list
}
Example 1
cntAround = 1, current = 5, cntPages = 9:
[1] ... [4] 5 [6] ... [9]
Example 2
cntAround = 3, current = 5, cntPages = 11:
[1] [2] [3] [4] 5 [6] [7] [8] ... [11]
1) Article is in Danish. Google Translate'd version is here.
Somewhat like this(pseudo-code):
pg = CurrentPageNo
low = 1
high = MAX_PAGES
if (pg-low <=5)
output 1 to pg-1 [with links]
else
output 1..3 [with links]
output "..."
output (pg-3) to (pg-1) [with links]
output pg
if (high - pg <=5)
output pg+1 to high [with links]
else
output (pg+1) to high-3 [with links]
output "..."
output (high-2) to high [with links]
You could use Zend_Paginator to do just that, and learn to use the Zend Framework while you're at it.
Below is php classes link from where you can download php class for pagination.
http://www.phpclasses.org/search.html?words=paging&x=0&y=0&go_search=1
If you (might potentially) have a large number of pages, consider using "logarithmic" page navigation, as described here (sample code included):
How to do page navigation for many, many pages? Logarithmic page navigation
(Note that it'll work just fine for small numbers of pages, too, of course!)