I want to make a minimalistic pagination script that basically does three things:
On first page, just a next button.
On last page, just a previous button.
For all others in between, both.
I have most of the code worked out, but I'm just making some if/elseif statements that determine which page the user is on and I'm having a bit of trouble. (those at bottom) First, here's the query code:
$per_page = 10;
$pages_query = mysql_query("SELECT COUNT(idnum) FROM images");
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] :1;
$start = ($page -1) * $per_page;
$query = mysql_query("SELECT * FROM images ORDER BY idnum DESC LIMIT $start, $per_page");
And here's the if statement part:
$nextend = $pages - 1;
$next = $page + 1;
$previous = $page - 1;
if ($pages >= 1 && $page = 1) {
echo 'next';
} elseif ($pages >= 1 && $page = 2) {
echo 'previous';
}
It always results in the next button, no matter what page I'm on. How do I detect the page number so I can display the pagination buttons the way I want to? By the way, I know I don't have the else statement for the middle pages (next and previous) yet.
You are assigning in your if statements rather than comparing. You don't want this in your if statement:
$page = 1
That just assigns 1 to $page.
You want this:
$page == 1
Or this:
$page === 1
$nextend = $pages - 1;
$next = $page + 1;
$previous = $page - 1;
$maxpages = ? //You need to have a variable with the last page number
if ($pages > 1) {
echo 'previous';
}
if ($page < $maxpages) {
echo 'next';
}
I don't understand what logic you are trying to do with your if/else statements, also when checking if a variable is equal to a number/other variable your "==" not "="
What you've got to do is way simple:
If you're not on the first page, you always have to show a back button.
If you're not on the last page you always have to show a next button.
Please note that both those conditions can happen at the same time so using an elseif between them won't work as it will only allow one of them to execute.
Example:
if ( $page > 1 )
{
echo( "Previous" );
}
if ( $page < $pages )
{
echo( "Next" );
}
Related
Here is my index.php code
My problem is my pagination has a continuous number of
pages.
<?php
$limit = 10;
$offset = (isset($_GET["page"]) ? $_GET["page"] - 1 : 0) * $limit;
$query = "SELECT * FROM employee ORDER BY employee_datecommenced ASC LIMIT
$offset,$limit ";
$list = getdata_inner_join($query);
?>
<?php
$total = $dbcon->query("SELECT count(*) FROM employee") or
die(mysqli_error());
$fetch = $total->fetch_assoc();
for($x = 0; $x < $fetch["count(*)"] / $limit ; $x ++){
$page = $x + 1;
if((isset($_GET["page"]) ? $_GET["page"] : 1) == $page)
$page = "<b>".$page."</b>";
echo ''.$page.' ';
}
?>
Click here to see the output photo
Thanks in advance
You say "My problem is my pagination has a continuous number of pages", but why is that a problem? What do you need to change?
in your for loop while printing the anchor tags, check the current page, if suppose the page is ($current), disable ($current-1), and print $current to ($current+5) and '....' and (current+1), if current is $min or $max manage the prev and next
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.
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);
this pagination only for next and previous wallpaper/ query , but its show also empty rows,
how to fix it?
<?php
// Figure out the limit for the query based
// on the current page number.
$from = (($walpaperid * $max_results) - $max_results);
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT( * ) AS `Rows` , `wallpaperid`
FROM `wallpaper` ORDER BY `wallpaper`.`wallpaperid` ASC"),0) or die(mysql_error());
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($from / $max_results);
// Build Page Number Hyperlinks
// Build Previous Link
if($wallpaperid > 1){
$prev = ($wallpaperid - 1);
echo "<Previous Page ";
}
for($i = 1; $i <= $total_pages; $i++){
if(($pagenum) == $i){
echo "<strong>$i</strong> ";
} else {
echo "<a class = 'mlnk' href=\"$siteurl3/$cat_url-$catid-$i.php\">$i</a> |";
}
}
// Build Next Link
if($wallpaperid < $total_pages){
$next = ($wallpaperid + 1);
echo "Next Page>";
}
?>
Correct your code to figure out the number of pages:
$total_pages = ceil($total_results / $max_results);
I'm displaying 10 records per page. The variables I have currently that I'm working with are..
$total = total number of records
$page = whats the current page I'm displaying
I placed this at the top of my page...
if ( $_GET['page'] == '' ) { $page = 1; } //if no page is specified set it to `1`
else { $page = ($_GET['page']); } // if page is specified set it
Here are my two links...
if ( $page != 1 ) { echo '<div style="float:left" ><a href="index.php?page='. ( $page - 1 ) .'" rev="prev" >Prev</a></div>'; }
if ( !( ( $total / ( 10 * $page ) ) < $page ) ) { echo '<div style="float:right" ><a href="index.php?page='. ( $page + 1 ) .'" rev="next" >Next</a></div>'; }
Now I guess (unless I'm not thinking of something) that I can display the "Prev" link every time except when the page is '1'. How can make it where the "Next" link doesn't show on the last page though?
replace your last line of code with:
if ($page*10 < $total) echo '<div style="float:right" ><a href="index.php?page='. ( $page + 1 ) .'" rev="next" >Next</a></div>';
!( ( $total / ( 10 * $page ) ) < $page )
I ... don't think this is correct.
Suppose $total is 1000 - then on page 99, 1000/990 is less than 99, but there's still another page to show.
What you probably want is to check whether 10 * $page is greater than or equal to $total.
You need the total divided by the pagecount ($total / 10) with an extra page if there is a remainder. Use the ceil function.
$page < ceil($total / 10)
Pagination is something that comes up so often in web projects, that it's usually best to create some kind of class somewhere, or at least some conveniences functions, that give you everything you need.
Here's a function that would return a dictionary of things that might be useful:
function pagination($numberItems, $perPage, $currentPage) {
$numPages = ceil($numberItems/$perPage);
return array(
'numberPages' => $numPages,
'start' => ($currentPage - 1) * $perPage + 1,
'end' => min($currentPage * $perPage, $numberItems),
'hasNext' => $page != $numPages,
'hasPrev' => $page != 1
);
}
You have a mistake in the last line:
$totalPages = ceil( $total / 10 );
if ( $page < $totalPages ) ) { echo "<div style="float:right" ><a href="index.php?page='. ( $page + 1 ) .'" rev="next" >Next</a></div>"; }
// lets get total number of pages here
$itemsPerPage = 10;
$totalItems = 105; // just for example
$pagesNo = ceil( $totalItems / $itemsPerPage );
// get the current page here...
$current = ( isset($_GET['page']) ) ? (int)$_GET['page'] : 1; // if page is defined, use it, instead use 1
$current = ( $current > 0 ) ? $current : 1; // if page was -1,-2... for instance, use 1
// show us 'Prev' button
echo ( $current > 1) ? "<a href='".($current-1)."'>Prev</a>" : "";
// show current page NO
echo $current;
// show 'Next' button
echo ( $current < $pagesNo) ? "<a href='".($current+1)."'>Next</a>" : "";
Should be like this... This is just quick code... Later, work on some page span (set how many pages should be shown before/after current page number...)
Sounds like you need to detect if you're on the first or the last page and "guard" the code against those conditions.
if (/* not the first page */) {
// print the prev link
}
if (/* not the last page */) {
// print the next link
}
You already have the code to detect if page=1 so now you just need to determine if you're on the last page or not (integer division or ceil() should be the way to go).
For pages of PHP results from SQL, there's a neat article on PHPFreaks http://www.phpfreaks.com/tutorial/basic-pagination