Php pagination link not showing on last page - php

I have a pagination script which works. The first page has a 'next' link, the last page should have a 'previous' link and the ones in between have both. The problem is that the last page doesnt show it 'previous' link.
Heres the code
include '../inc/connect.php';
//paganation settings
$per_page = 2;
$pages_query = mysqli_query($link, "SELECT COUNT(`id`) FROM `gallery`") or
die(mysqli_error($link));
$result = mysqli_fetch_array($pages_query, MYSQLI_NUM);
$pages = $result[0] / $per_page;
$page = (isset($_GET['page']) AND (int)$_GET['page'] > 0) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$last = ($pages - 1) / $per_page;
$prev = $page - 1;
$next = $page + 1;
//query the db
$q =mysqli_query($link, "SELECT * FROM gallery
ORDER BY id DESC
LIMIT $start, $per_page");
//find out the page we are on and display next and previous links accordingly
if ($page >= 1 && $page < $pages){
echo "<span class='next'>";
echo "Next page ";
echo "</span>";
if ($page >=2){
echo "<span class='prev'>";
echo "Previous page ";
echo "</span>";
}
}
else if ($page > $pages + 1){
echo 'No more images in the database';
}
//display images
while($row=mysqli_fetch_array($q)){
echo "<a href='{$row['filename']}' rel='thumbnail'>
<img class='nailthumb-container' src='{$row['filename']}'
alt='{$row['description']}.Image' title='{$row['description']}' />
</a>";
}

The last page link is falling under the ($page < $pages) test, and it probably shouldn't.
if ($page >= 1){
if ($page < $pages) {
echo "<span class='next'>";
echo "Next page ";
echo "</span>";
}
if ($page >=2){
echo "<span class='prev'>";
echo "Previous page ";
echo "</span>";
}
}

Change it to this will work i think, on your last page $page is equal to $pages instead of smaller than $pages:
if ($page >= 1 && $page <= $pages){...}

Related

How to Add Previous Page, Next Page, First Page, Last Page in the Pagination?

I want to know how can i add next page, previous page, first page and last page in this pagination. Below code is only showing the page number it doesn't show next page, previous page, first page and last page.
<?php
$pagesToShow = 10;
$pageSize = 20;
$numPages = ceil($numResults / $pageSize);
$pageLefts = min($pagesToShow, $numPages);
$currentPage = $page - floor( $pagesToShow / 2 );
if($currentPage < 1){
$currentPage = 1;
}
if($currentPage + $pageLefts > $numPages + 1) {
$currentPage = $numPages + 1 - $pageLefts;
}
while($pageLefts != 0 && $currentPage <= $numPages) {
if($currentPage == $page){
echo "<div class='pageNumberContainer'>
<span class='pageNumber'>$currentPage</span>
</div>";
}else{
echo "<div class='pageNumberContainer'>
<a href='search.php?term=$term&type=$type&page=$currentPage'>
<span class='pageNumber'>$currentPage</span>
</a>
</div>";
}
$currentPage++;
$pageLefts--;
}
?>
I dont know if this helps .. but maybe you can do something with it :)
<?php $pagesToShow = 10;
$pageSize = 20;
$numPages = ceil($numResults / $pageSize);
$pageLefts = min($pagesToShow, $numPages);
$currentPage = $page - floor( $pagesToShow / 2 );
if($currentPage < 1){
$currentPage = 1;
}
if($currentPage + $pageLefts > $numPages + 1) {
$currentPage = $numPages + 1 - $pageLefts;
}
if ($numPages > 1){echo 'FirstPage = 1';}
while($pageLefts != 0 && $currentPage <= $numPages) {
if($currentPage == $page){
echo "<div class='pageNumberContainer'>
<span class='pageNumber'>Current Page Number Is: ".$currentPage</span>
</div>";
}else{
if ($currentPage != 1){$previousPage = $currentPage - 1; echo
'PREVIOUS PAGE:' . $previousPage; }
echo "<div class='pageNumberContainer'>
<a href='search.php?term=".$term."&type=".$type."&page=".$currentPage."'>
<span class='pageNumber'>CURRENT PAGE: ".$currentPage." </span>
</a>
</div>";
if ($currentPage != $numPages){$nextPage = $currentPage + 1; echo 'NEXT PAGE:' . $nextPage; }
}
$currentPage++;
$pageLefts--;
}
if ($numPages > 1){echo 'LastPage = '.$numPages;}
?>

php pagination on second page not working

$dwdb=mysqli_connect("localhost","root","","dw");
//this is my page number
$page=(isset($_GET['page']) && $_GET['page']>0)?$_GET['page']:1 ;
//this is my cat_id
$new=(isset($_GET['year']) && $_GET['year']>0)?$_GET['year']:1 ;
$perpage=2;
$limit=($page > 1)?($page*$perpage)-$perpage:0;
$query=mysqli_query($dwdb,"select *from movies where y_id='$new' limit {$limit},{$perpage}");
while($result=mysqli_fetch_array($query)){
$id=$result['m_id'];
$name=$result['title'];
$img=$result['image'];
echo"<div><a href='downloadpage.php?yc=$id'>$id.....$name<br><img src='i/image/$img' style='height:200px;width:200px;'/></a></div>";
}
$query1=mysqli_query($dwdb,"select *from movies where y_id='$new'");
$total=mysqli_num_rows($query1);
$pages=ceil($total/$perpage);
echo "<a href='index1.php?page=1'>".'First Page'."</a>";
for ($i=1; $i<=$pages;$i++){
echo "<a href='index1.php?page=".$i."'>".$i."</a> ";
};
echo "<a href='index1.php?page=$pages'>Last page</a>";
That is my code. The problem is that on my second page i have result of first $new variable .......................................................................................................................................................................
... i have a problem that on my second page i have result of first $new variable
That's because you're not including $new variable in the pagination links. So every time you go to 2nd, 3rd, 4th, ... page, you'll get the same $new value as 1, and that's because of this statement,
$new=(isset($_GET['year']) && $_GET['year']>0)?$_GET['year']:1 ;
Include this variable in the pagination links so that you could get it's value in the subsequent pages. So your pagination links section would be like this:
// your code
echo "<a href='index1.php?page=1&year=".$new."'>".'First Page'."</a>";
for ($i=1; $i<=$pages;$i++){
echo "<a href='index1.php?page=".$i."&year=".$new."'>".$i."</a> ";
}
echo "<a href='index1.php?page=".$pages."&year=".$new."'>Last page</a>";
for pagination use this code in file with name: view-paginated.php
$per_page = 10;
$result = mysql_query("SELECT * FROM table");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
$start = 0;
$end = $per_page;
}
}
else
{
$start = 0;
$end = $per_page;
}
echo "<p><a href='view.php'>show all</a> | <b>page:</b> ";
for ($i = 1; $i <= $total_pages; $i++)
{
echo "<a href='view-paginated.php?page=$i'>$i</a> ";
}
for ($i = $start; $i < $end; $i++)
{
if ($i == $total_results) { break; }
echo mysql_result($result, $i, 'YOUR COLUMN') ;
}

show page numbering in PHP

I am using this coed in PHP to show next and previous buttons for records in a mysql database:
$sql="SELECT * from customer";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$MaxRowsPerPage = 25;
$total_records = mysql_num_rows($rs);
$total_pages = ceil($total_records / $MaxRowsPerPage);
if(isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page=1;
}
$start_from = ($page-1) * $MaxRowsPerPage;
$sql.=" LIMIT $start_from, $MaxRowsPerPage";
I am echoing $total_records to show the total amount, how can i show the number from and to on the current page. for example, on page 1 it will be showing records 1 to 25 (because max rows per page is 25) and then page 2 will be showing records 26 to 50 and so on...
There a are many ways of doing this, but here's a simple pagination example I made. It will also show 1-25, 26-50 etc. It's heavily commented so it should be easy to understand.
<?php
// Connect to database
include 'includes/db_connect.php';
// Find total number of rows in table
$result = mysql_query("SELECT COUNT(*) FROM example_table");
$row = mysql_fetch_array($result);
$total_rows = $row[0];
// Set rows per page
$rows_per_page = 25;
// Calculate total number of pages
$total_pages = ceil($total_rows / $rows_per_page);
// Get current page
$current_page = (isset($_GET['p']) && $_GET['p'] > 0) ? (int) $_GET['p'] : 1;
// If current page is greater than last page, set it to last.
if ($current_page > $total_pages)
$current_page = $total_pages;
// Set starting post
$offset = ($current_page - 1) * $rows_per_page;
// Get rows from database
$result = mysql_query("SELECT * FROM example_table LIMIT $offset, $rows_per_page");
// Print rows
echo '<hr>';
while($row = mysql_fetch_array($result))
{
echo $row['id'].'<br />';
echo $row['text'];
echo '<hr>';
}
// Build navigation
// Range of pages on each side of current page in navigation
$range = 4;
// Create navigation link for previous and first page.
if ($current_page > 1)
{
$back = $current_page - 1;
echo ' PREV ';
if ($current_page > ($range + 1))
echo ' 1... ';
}
else
echo ' PREV ';
// Create page links, based on chosen range.
for ($i = $current_page - $range; $i < ($current_page + $range) + 1; $i++)
{
if ($i > 0 && $i <= $total_pages)
if ($i == $current_page)
echo ' [<strong>'.$i.'</strong>] ';
else
echo ' '.$i.' ';
}
// Create navigation link for next and last page.
if ($current_page != $total_pages)
{
$next = $current_page + 1;
if (($current_page + $range) < $total_pages)
echo ' ...'.$total_pages.' ';
echo ' NEXT ';
}
else
echo ' NEXT ';
?>

PHP Pagination of MySQL Query not incrementing/decrementing properly

I'm having trouble with the follow PHP which paginates the results of a MySQL query.
When I go to webpagename.php with the first page of the results and click Previous, the browser changes to webpagename.php?page=-1 and shows the first page of results again. If I click Previous again, it changes to webpagename.php?page=-2 and shows Page 1 of the results again, etc.
When I go to webpagename.php with the first page of the results and click Next, the browser changes to webpagename.php?page=1 and shows the first page of results again. I then have to hit Next a second time to move to Page 2.
When I go to the last page of the results - Page 8 - and click Next, the browser changes to webpagename.php?page=9 and shows Page 1 of the results. If I click Next again, it shows webpagename.php?page=10 and shows Page 1 of the results again, etc.
Expected Results:
When on Page 1 and a user hits Previous, I would like the code to do nothing/not decrement. When on Page 8 - the last page of results, I would like the code to do nothing/not increment. Of course, I would also expect that if you hit Next from Page 1 that it doesn't display Page 1 a second time but rather goes to Page 2.
Your exact changes to this code to make it work properly are very much appreciated. Thank you for time.
<?php
mysql_connect("localhost","username","password") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());
// number of results to show per page
$per_page = 10;
// figure out the total pages in the database
$result = mysql_query("SELECT * FROM uc_users LEFT JOIN ent_dancers ON uc_users.id = ent_dancers.id WHERE ent_dancers.DancerYesNo = '1' AND ent_dancers.DancerEnabledYesNo = '1' ORDER BY uc_users.display_name ASC");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);
// check if the 'page' variable is set in the URL (ex: webpagename.php?page=1)
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
// make sure the $show_page value is valid
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
// display data in table
echo "<div class='dancerbio'>";
echo "<div class='uts-1'>";
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++)
{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }
// echo out the contents of each row into a table
$rowid = mysql_result($result, $i, 'id');
echo "<div class='uts-1-1'><a class='bodytxt5' href='webpagename-details.php?userid=$rowid'>" . mysql_result($result, $i, 'display_name') . "</a></div>";
}
// close table>
echo "<div class='ugen-1'></div>";
echo "</div>";
$prev = $_GET['page'] - 1;
echo "<div style='clear:both;height:1px;overflow: hidden;'></div>";
echo "<br /><a class='bodytxt5' href='webpagename.php?page=" . $prev . "'>Prev</a> ";
for ($i = 1; $i <= $total_pages; $i++)
{
echo "<a class='bodytxt5' href='webpagename.php?page=$i'>$i</a> ";
}
$next = $_GET['page'] + 1;
echo " <a class='bodytxt5' href='webpagename.php?page=" . $next . "'>Next</a> ";
echo "</div>";
// pagination
?>
replace this:
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
// make sure the $show_page value is valid
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
by this:
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
elseif ($show_page > $total_pages)
{
$show_page=$total_pages;
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else {
$show_page=1;
$start = 0;
$end = $per_page;
}
}
else {
$show_page=1;
$start = 0;
$end = $per_page;
}
then :
$prev=$show_page-1;
$next=$show_page+1;
if($show_page>1){//this way previsous won't appear if you are at page 1 already
//show previous div
}
if($show_page<$total_pages){ //this way next won't appear unless you are not at the last page
//show next div
}
Make a variable $page set it equal to $_GET['page'].
if(isset($_GET['page'])){
$page = $_GET['page'];
}
else{
$page = 1;
}
you need to put a condition before echoing previous link to check whether $_GET['page'] is set or not and is greater than 1.
Like this:
if($page!=($start+1)){
$prev = $page - 1;
echo "<div style='clear:both;height:1px;overflow: hidden;'></div>";
echo "<br /><a class='bodytxt5' href='webpagename.php?page=" . $prev . "'>Prev</a> ";
}
Add another condition for next
if($page!=$total_pages)
{
$next = $page+1
echo " <a class='bodytxt5' href='webpagename.php?page=" . $next . "'>Next</a> ";
echo "</div>";
}
I hope your issue is solved.
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
// make sure the $show_page value is valid
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
$show_page=1
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
$show_page=1;
}
// display pagination
// display data in table
echo "<div class='dancerbio'>";
echo "<div class='uts-1'>";
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++)
{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }
// echo out the contents of each row into a table
$rowid = mysql_result($result, $i, 'id');
echo "<div class='uts-1-1'><a class='bodytxt5' href='webpagename-details.php?userid=$rowid'>" . mysql_result($result, $i, 'display_name') . "</a></div>";
}
// close table>
echo "<div class='ugen-1'></div>";
echo "</div>";
if($show_page!=($start+1)){
$prev = $page - 1;
echo "<div style='clear:both;height:1px;overflow: hidden;'></div>";
echo "<br /><a class='bodytxt5' href='webpagename.php?page=" . $prev . "'>Prev</a> ";
}
for ($i = 1; $i <= $total_pages; $i++)
{
echo "<a class='bodytxt5' href='webpagename.php?page=$i'>$i</a> ";
}
if($show_page!=$total_pages)
{
$next = $page+1
echo " <a class='bodytxt5' href='webpagename.php?page=" . $next . "'>Next</a> ";
echo "</div>";
}
// pagination
?>

PHP Pagination with x amount of pages

Currently I have a functioning pagination script, although I'm missing on feature. At the moment it's possible for hundreds of pages to be showing in the $pages list, because there's no filter on to show between, for example, [1] [...] 5, 6, 7 [..] [45]. Here's my code:
/** Pagination **/
$limit = 7;
$query = "SELECT COUNT(*) FROM users";
$result = $db->prepare($query);
$result->execute();
$pages_query = $result->fetchColumn(0);
$count = number_format($pages_query);
$pages = ceil($pages_query / $limit);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $limit;
ob_start();
echo("<span class='alignright'>");
if ($pages >= 1 && $page <= $pages){
if($page > 1){
$next = ($page - 1);
$link = "?page=$next";
echo("<a class='paginate' href='$link'><i class='icon-caret-left'></i></a>");
}
for ($x=1; $x<=$pages; $x++){
echo ($x == $page) ? "<strong style='font-weight: bold!important;'><a class='paginate' href='?page=$x'>$x</a></strong>" : "<a class='paginate' href='?page=$x'>$x</a>";
}
if($page < $pages){
$next = ($page + 1);
$link = "?page=$next";
echo("<a class='paginate' href='$link'><i class='icon-caret-right'></i></a>");
}
echo("</span>");
if($count > 0){
echo("<span class='smalltext'>Page <strong class='half'>$page</strong> of $pages:</span>");
} else {
echo("<span class='smalltext'>There are <span class='half'>$count</span> results to display.</span>");
}
$pagintion = ob_get_clean();
(It's been stripped from other junk that was in it but that's the general frame.) Basically I'm trying to figure out how to limit it to have "between pages" down the bottom as specified in the top part of the question. Something like:
[<] [1] ... [4] [5] [6] ... [45] [>]
If that makes sense.
you can try LIMIT with in your query itself
SELECT * FROM TABLE_NAME LIMIT STARTING_RESULT_NUMBER, RESULTS_PER_PAGE
RESULTS_PER_PAGE is no.of items per page you want to display
STARTING_RESULT_NUMBER =(CURRENT_PAGE_NUMBER*RESULTS_PER_PAGE)
In order to change the list of pages to something like [<] [1] ... [4] [5] [6] ... [45] [>] rather than showing all pages numbers, you can replace your for loop by something like :
echo "<a class='paginate' href='?page=1'>1</a>";
if($page > 3) {echo "...";}
if($page > 2) {echo "<a class='paginate' href='?page=" . $x-1 . "'>" . $x-1 "</a>";}
if($page != 1 && $page != pages) {echo "<a class='paginate' href='?page=" . $x . "'>" . $x "</a>";}
if($page < $pages-1) {echo "<a class='paginate' href='?page=" . $x+1 . "'>" . $x+1 "</a>";}
if($page < $pages-2) {echo "...";}
if($pages >1) {echo "<a class='paginate' href='?page=1'>1</a>";}
Il will show the first page, the last pages, the current pages and the ones just before and just after.
My personal meaning with pagination is that it must be readable and simple to change later on.
I typed the following code based on you're example:
$totalPages = 145; //the total amount of pages
$selectedPage = 40; //the selected page
$pages = array(); //the array which is gonna hold the pages we need to display
$offset = 3; //the number of pages to select around the selected page
$closePages = range($selectedPage - $offset, $selectedPage + $offset); //select the pages that are in $offset of the selected page
array_filter($closePages, function($x) { //filter the pages below 1 and above $totalPages
return ($x <= $totalPages && $x >= 1 ? true : false );
});
array_push($pages, 1); //add the first page
array_push($pages, '...'); //add some dots
$pages = array_merge($pages, $closePages);
array_push($pages, '...'); //and again add some dots
array_push($pages, $totalPages); //add the last page
Then you use a foreach loop to display the pages:
foreach($pages as $page) {
if (is_numeric($page)) {
if ($page != $selectedPage) $content .= ' ' . $page . ' ';
else $content .= ' <strong>' . $page . '</strong> ';
} else
$content .= '[...]';
}
Some explanation after you're comment on this answer:
The $totalPages variable must be the total amount of pages on the page (from you're example) and the $selectedPage is the page that is selected at this moment.
$totalPages = ceil($pages_query / $limit);
$selectedPage = isset($_GET['page']) ? $_GET['page'] : 1;

Categories