I have a simple navigation php script which works ok.
The only problem is that I need to find a way to display the pagination links on top of the page instead of the bottom.
So for me a newb, it seems impossible since I have to show the pagination links before the actual navigation variables are being declared.
Can this be done?
Here is the script:
<?
$numrows = '600';
$rowsperpage = 20;
$totalpages = ceil($numrows / $rowsperpage);
if ($currentpage > $totalpages){ $currentpage = $totalpages; }
if ($currentpage < 1){ $currentpage = 1; }
$offset = ($currentpage - 1) * $rowsperpage;
HERE I query the data from the table and basically fill in the page.
Now starts the pagination links:
$range = 3;
if ($currentpage > 1) {
echo 'First';
}
$prevpage = $currentpage - 1;
echo 'Previous';
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if($x == $currentpage){
echo '<span class="current">'.$x.'</span>';
} else {
echo ''.$x.'';
}
}
}
if ($currentpage != $totalpages){
$nextpage = $currentpage + 1;
echo 'Next';
echo 'Last';
}
?>
When I do anything in PHP I do the following
<?php
//Calculate anything I need for the page to be displayed
//Build the html page and fill in variables where needed
?>
You may also want to look into frameworks like smarty that separate the logic from the template designs. PHP is really a templating language in itself but I actually quite like using smarty.
Related
This is the first pagination script I've coded with help from a friend. I customized it a bit and want to also add NEXT and PREVIOUS buttons. Here is my code.
$per_page = 6;
$pages_query = mysql_query("SELECT COUNT(`user_id`) FROM `users`");
$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 `username` FROM `users` WHERE `active` = 1 LIMIT $start, $per_page");
while ($query_row = mysql_fetch_assoc($query)) {
echo '<p>', $query_row['username'] , '</p>';
}
//previous (this is where the previous button would go)
if ($pages >=1 && $page <=$pages) {
for ($x=1; $x<=$pages; $x++) {
echo ($x == $page) ? '<strong>'.$x.'</strong> ' : ''.$x.' ';
}
}
I've simplified the script and have removed the last page first page buttons. I also exluded the php tags and mysql connection
This is pretty simple
add this before loop starts with $_GET global value
for your previous
if(isset($_GET['page']) && $_GET['page'] > 1){ href($_GET['page'] -1) }
for your next
if(isset($_GET['page']) && $_GET['page'] < $pages){ href($_GET['page'] +1) }
you can add this with subtract and add operator
however thanks for sharing the code i got really easy way to use your code with my custom php application too :)
i have a paging script
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nad .= " $page "; // no need to create a link to current page
}
else
{
$nad .= " $page ";
}
}
that will show pages for the search query. I want to limit the amount of pages it shows to 5, because currently it shows every page, which is a problem,
say if there were 5000 rows and 5 rows per page, it would show 1000 pages. How do i limit that to 5?
You can use the min() and the max() functions:
$firstPage = max(1, $pageNum-5);
$lastPage = min($maxPage, $pageNum+5);
for($page = $firstPage; $page <= $lastPage; $page++)
{
// no changes here
}
i have more data's in my mysql database. i want to display the data, per page 10 data only i need to display, for this i wrote pagination code. its working very fine but i want to run that pagination automatically that means automatically after few seconds the page turns to second page then third page etc... but i don't know how to implement please help anyone. Here below the sample code for reference:
<?php
include "config.inc";
$sql = "SELECT COUNT(*) FROM test";
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
$rowsperpage = 3;
$totalpages = ceil($numrows / $rowsperpage);
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
$currentpage = (int) $_GET['currentpage'];
} else {
$currentpage = 1;
}
if ($currentpage > $totalpages) {
$currentpage = $totalpages;
}
if ($currentpage < 1) {
$currentpage = 1;
}
$offset = ($currentpage - 1) * $rowsperpage;
$sql = "SELECT * FROM test LIMIT $offset, $rowsperpage";
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
while ($list = mysql_fetch_array($result)) {
echo $list['mark_cut_weld'] . " : " . $list['mark_cut_inves'] . "<br />";
}
$range = 3;
if ($currentpage > 1) {
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
$prevpage = $currentpage - 1;
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
}
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
echo " [<b>$x</b>] ";
} else {
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
}
}
}
if ($currentpage != $totalpages) {
$nextpage = $currentpage + 1;
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
}
?>
Above code, i just fetch the data from mysql database by php. then set the data per page is 3. i just get the total count and then divide by number of rows into rows per page... then automatically it will display the data.
My target is display the data from database. per page 10 data's and then automatically it move to next page for next 10 data without any action click or submit...
Because it is status board program.. we going to display by big tv in factory... so workers can see the status of the work in this big tv.
You can set a header redirect to redirect to next page.
For example the following code will redirect you to next page in 10 seconds.
header('Refresh: 10; URL='.$_SERVER['PHP_SELF'].'?page='.$next_page);
Make sure you set the header before you echo anything in PHP.
You are going to want to use javascript to set timeouts that will redirect the location to the next page every so often.
For example, add this to the bottom of your HTML body:
<script type="text/javascript">
function switchPage(){
window.location = "<?php echo $next_page?>"; // set the next page of results to view.
}
setTimeout(switchPage,60*1000); // call callback every minute
</script>
The variable $next_page will need to be a URL to the next set of results using PHP.
To have it repeat you will need a modulus on the PHP side that flips back to page 0 when the end of the results has been reached.
<?php
$next_page_count = ++$currentpage % $totalpages;
$next_page = $_SERVER['PHP_SELF'] . '?currentpage=' . $next_page_count;
I was wondering if anybody could help me out with a bit of a pagination issue in my store. Using pajinate, I initially put this together but it seems to be bugged. I'd like to add a 'previous' and 'back to beginning' as well as a 'next' and 'last page' button. I tried implementing the 'back to beginning' and 'previous' buttons first but they aren't working correctly. If anybody could help me out a bit that would be fantastic. On a side note, i have this set to display a range of 10 numbers at a time, but each time i click on a higher page number it appends with 4-5 more numbers any insight on this issue would be greatly appreciated as well.
<?php
// find out total pages
$totalpages = $resp->paginationOutput->totalPages;
echo "Total Results: ";
echo $totalpages.' pages ';
// get the current page or set a default
if (isset($_GET['pgno']) && is_numeric($_GET['pgno'])) {
// cast var as int
$currentpage = (int) $_GET['pgno'];
} else {
// default page num
$currentpage = 1;
} // end if
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $entriesPerPage;
/****** build the pagination links ******/
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
if(null!=$queryString){
// echo " <a href='{$_SERVER['PHP_SELF']}?$queryString¤tpage=1'><<</a> ";
}else{
// echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
}
// get previous page num
// $prevpage = $currentpage - 1;
// show < link to go back 1 page
// echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if
// range of num links to show
$range = 10;
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<strong>$x</strong>] ";
// if not current page...
} else {
// make it a link
echo " <a href='".add_url_param('pgno',$x)."'>$x</a> ";
} // end else
} // end if
} // end for
?>
you had prev page commented out, and using $_GET['currentpage'] rather than $_GET['pgno'], this worked for me:
<?
$totalpages = 10;
if (isset($_GET['pgno']) && is_numeric($_GET['pgno'])) {
// cast var as int
$currentpage = (int) $_GET['pgno'];
} else {
// default page num
$currentpage = 1;
} // end if
echo $currentpage;
echo "/".$totalpages;
echo "<br />";
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $entriesPerPage;
/****** build the pagination links ******/
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
if(null!=$queryString){
echo " <a href='{$_SERVER['PHP_SELF']}?$queryString&pgno=1'><<</a> ";
}else{
echo " <a href='{$_SERVER['PHP_SELF']}?pgno=1'><<</a> ";
}
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?pgno=$prevpage'><</a> ";
} // end if
// range of num links to show
$range = 10;
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<strong>$x</strong>] ";
// if not current page...
} else {
// make it a link
echo " <a href='".$_SERVER['PHP_SELF']."?pgno=$x"."'>$x</a> ";
} // end else
} // end if
} // end for
?>
$offset = ($currentpage - 1) * $entriesPerPage;
if ($currentpage > 1) {
?where comes this variable
Hi i am a new programmer.
i have a simple pagination which works fine,
i have sorting options for sorting my recordset result by id, title etc which is also working fine, the codes are below.
now i want to combine both and have a functionality that pagination should work on both conditions.
that is when recordset result is displayed by default pagination should work as before.
and when recordset result is sorted by options pagination should work on sorted result too.
i got something figured out, the codes are below, but i cant get it to work.
my code for recordset and basic pagination-sorting are:
<?php
$table='mytable';
$pagename = "is-test.php";
$db = mysql_select_db($database,$connection) or trigger_error("SQL", E_USER_ERROR);
$sql1 = "SELECT COUNT(*) FROM $table";
$result1 = mysql_query($sql1, $connection) or trigger_error("SQL", E_USER_ERROR);
$row = mysql_fetch_row($result1);
$numrows = $row[0];
$rowsperpage = 5;
$totalpages = ceil($numrows / $rowsperpage);
if (isset($_GET['page']) && is_numeric($_GET['page'])) {
$currentpage = (int) mysql_real_escape_string($_GET['page']);
} else {
$currentpage = 1;
}
if ($currentpage > $totalpages) {
$currentpage = $totalpages;
}
if ($currentpage < 1) {
$currentpage = 1;
}
$orderBy = array('id', 'title',);
$order = '';
if (isset($_GET['orderBy']) && in_array($_GET['orderBy'], $orderBy)) {
$order = mysql_real_escape_string($_GET['orderBy']);
}else{
$order='id';
}
$offset = ($currentpage - 1) * $rowsperpage;
$sql2 = "SELECT * FROM $table ORDER BY $order ASC LIMIT $offset, $rowsperpage";
$result2 = mysql_query($sql2, $connection) or trigger_error("SQL", E_USER_ERROR);
$list = mysql_fetch_assoc($result2);
$startrow = ($currentpage-1) * $rowsperpage
my codes for sorting options:
Sort by
id:
title:
my codes for pagination are:
if ($currentpage != $totalpages) {
$nextpage = $currentpage + 1;
echo " <li><a href='$pagename?page=$nextpage'>Next»»</a></li> ";
}
if($currentpage<$totalpages){
for ($x = ($currentpage - 3); $x < (($currentpage + 3) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
echo " <li id='pcurrent'><a href='$pagename?page=$x'>$x</a></li>";
} else {
echo " <li><a href='$pagename?page=$x'>$x</a></li> ";
}}}
}
if ($currentpage > 1){
$prevpage = $currentpage - 1;
echo " <li><a href='$pagename?page=$prevpage'>««Prev</a></li> ";
}
upto now everytthing is working except pagination only paginate defaut recordset resunt not sorted recordset result for that i have to change url parameter for pagination to work on sorting result,
so i have changed my pagination links as
if ($currentpage != $totalpages) {
$nextpage = $currentpage + 1;
echo " <li><a href='$pagename?orderBy=$order,page=$nextpage'>Next»»</a></li> ";
}
if($currentpage<$totalpages){
for ($x = ($currentpage - 3); $x < (($currentpage + 3) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
echo " <li id='pcurrent'><a href='$pagename?orderBy=$order,page=$x'>$x</a></li>";
} else {
echo " <li><a href='$pagename?orderBy=$order,page=$x'>$x</a></li> ";
}}}
}
if ($currentpage > 1){
$prevpage = $currentpage - 1;
echo " <li><a href='$pagename?page=$prevpage'>««Prev</a></li> ";
}
i.e added orderBy=$order in links for pagination, but pagination is not working now,
not on default recordset result and not on sorted recordset result.
please see what i am doing wrong
I didn't read the whole code but you have at least one error in your query string. Each GET-parameter should be separated by "&" instead of "," what you did. Changing your code from
'?orderBy=$order,page=$nextpage'
to
'?orderBy=$order&page=$nextpage'
should fix this error at least.
You should put error_reporting(E_ALL); at the beginning of your code to see notices from php (which would have helped you a lot). If it's still not working afterwards you should debug your GET params with
<?php var_dump($_GET); ?>