If you have a simple php and mysql DATA paging program, please refer to it.
| The Code is as follows: |
Copy code |
|
<? Php
// Adam's Custom PHP MySQL Pagination Tutorial and Script
// You have to put your mysql connection data and alter the SQL queries (both queries)
// This script is in tutorial form and is accompanied by the following video:
Mysql_connect ("DB_Host_Here", "DB_Username_Here", "DB_Password_Here") or die (mysql_error ());
Mysql_select_db ("DB_Name_Here") or die (mysql_error ());
///// // Query the member data initially like you normally wowould
$ SQL = mysql_query ("SELECT id, firstname, country FROM myTable ORDER BY id ASC ");
/// // Adam's Pagination logic /////////////////////////////////////// /////////////////////////////////
$ Nr = mysql_num_rows ($ SQL); // Get total of Num rows from the database query
If (isset ($ _ GET ['pn ']) {// Get pn from URL vars if it is present
$ Pn = preg_replace ('# [^ 0-9] # I', '', $ _ GET ['pn ']); // filter everything but numbers for security (new)
// $ Pn = ereg_replace ("[^ 0-9]", "", $ _ GET ['pn ']); // filter everything but numbers for security (deprecated)
} Else {// If the pn URL variable is not present force it to be value of page number 1
$ Pn = 1;
}
// This is where we set how to configure database items to show on each page
$ ItemsPerPage = 10;
// Get the value of the last page in the pagination result set
$ LastPage = ceil ($ nr/$ itemsPerPage );
// Be sure URL variable $ pn (page number) is no lower than page 1 and no higher than $ lastpage
If ($ pn <1) {// If it is less than 1
$ Pn = 1; // force if to be 1
} Else if ($ pn> $ lastPage) {// if it is greater than $ lastpage
$ Pn = $ lastPage; // force it to be $ lastpage's value
}
// This creates the numbers to click in between the next and back buttons
// This section is explained well in the video that accompanies this script
$ CenterPages = "";
$ Sub1 = $ pn-1;
$ Sub2 = $ pn-2;
$ Add1 = $ pn + 1;
$ Add2 = $ pn + 2;
If ($ pn = 1 ){
$ CenterPages. = '& nbsp; <span class = "pagNumActive">'. $ pn. '</span> & nbsp ;';
$ CenterPages. = '& nbsp; <a href = "'. $ _ SERVER ['php _ SELF '].'? Pn = '. $ add1.' "> '. $ add1.' </a> & nbsp ;';
} Else if ($ pn ==$ lastPage ){
$ CenterPages. = '& nbsp; <a href = "'. $ _ SERVER ['php _ SELF '].'? Pn = '. $ sub1.' "> '. $ sub1.' </a> & nbsp ;';
$ CenterPages. = '& nbsp; <span class = "pagNumActive">'. $ pn. '</span> & nbsp ;';
} Else if ($ pn> 2 & $ pn <($ lastPage-1 )){
$ CenterPages. = '& nbsp; <a href = "'. $ _ SERVER ['php _ SELF '].'? Pn = '. $ sub2.' "> '. $ sub2.' </a> & nbsp ;';
$ CenterPages. = '& nbsp; <a href = "'. $ _ SERVER ['php _ SELF '].'? Pn = '. $ sub1.' "> '. $ sub1.' </a> & nbsp ;';
$ CenterPages. = '& nbsp; <span class = "pagNumActive">'. $ pn. '</span> & nbsp ;';
$ CenterPages. = '& nbsp; <a href = "'. $ _ SERVER ['php _ SELF '].'? Pn = '. $ add1.' "> '. $ add1.' </a> & nbsp ;';
$ CenterPages. = '& nbsp; <a href = "'. $ _ SERVER ['php _ SELF '].'? Pn = '. $ add2.' "> '. $ add2.' </a> & nbsp ;';
} Else if ($ pn> 1 & $ pn <$ lastPage ){
$ CenterPages. = '& nbsp; <a href = "'. $ _ SERVER ['php _ SELF '].'? Pn = '. $ sub1.' "> '. $ sub1.' </a> & nbsp ;';
$ CenterPages. = '& nbsp; <span class = "pagNumActive">'. $ pn. '</span> & nbsp ;';
$ CenterPages. = '& nbsp; <a href = "'. $ _ SERVER ['php _ SELF '].'? Pn = '. $ add1.' "> '. $ add1.' </a> & nbsp ;';
}
// This line sets the "LIMIT" range... the 2 values we place to choose a range of rows from database in our query
$ Limit = 'limit'. ($ pn-1) * $ itemsPerPage. ','. $ itemsPerPage;
// Now we are going to run the same query as abve but this time add $ limit onto the end of the SQL syntax
// $ Sql2 is what we will use to fuel our while loop statement below
$ Sql2 = mysql_query ("SELECT id, firstname, country FROM myTable order by id ASC $ limit ");
/// // END Adam's Pagination Logic // //////////////////////////////////////// //////////////////////////////////////
/// // Adam's pagination Display Setup ///////////////////////////////////// ////////////////////////////////
$ PaginationDisplay = ""; // Initialize the pagination output variable
// This code runs only if the last page variable is ot equal to 1, if it is only 1 page we require no paginated links to display
If ($ lastPage! = "1 "){
// This shows the user what page they are on, and the total number of pages
$ PaginationDisplay. = 'page <strong> '. $ pn.' </strong> of '. $ lastPage.' & nbsp ;';
// If we are not on page 1 we can place the Back button
If ($ pn! = 1 ){
$ Previous = $ pn-1;
$ PaginationDisplay. = '& nbsp; <a href = "'. $ _ SERVER ['php _ SELF '].'? Pn = '. $ previous.' "> Back </a> ';
}
// Lay in the clickable numbers display here between the Back and Next links
$ PaginationDisplay. = '<span class = "paginationNumbers">'. $ centerPages. '</span> ';
// If we are not on the very last page we can place the Next button
If ($ pn! = $ LastPage ){
$ NextPage = $ pn + 1;
$ PaginationDisplay. = '& nbsp; <a href = "'. $ _ SERVER ['php _ SELF '].'? Pn = '. $ nextPage.' "> Next </a> ';
}
}
/// // END Adam' s Pagination Display Setup //////////////////////////////////// ///////////////////////////////////////
// Build the Output Section Here
$ OutputList = '';
While ($ row = mysql_fetch_array ($ sql2 )){
$ Id = $ row ["id"];
$ Firstname = $ row ["firstname"];
$ Country = $ row ["country"];
$ OutputList. = '
} // Close while loop
?>
<Html>
<Head>
<Title> Adam's Pagination </title>
<Style type = "text/css">
<! --
. PagNumActive {
Color: #000;
Border: #060 1px solid; background-color: # D2FFD2; padding-left: 3px; padding-right: 3px;
}
. PaginationNumbers a: link {
Color: #000;
Text-decoration: none;
Border: #999 1px solid; background-color: # F0F0F0; padding-left: 3px; padding-right: 3px;
}
. PaginationNumbers a: visited {
Color: #000;
Text-decoration: none;
Border: #999 1px solid; background-color: # F0F0F0; padding-left: 3px; padding-right: 3px;
}
. PaginationNumbers a: hover {
Color: #000;
Text-decoration: none;
Border: #060 1px solid; background-color: # D2FFD2; padding-left: 3px; padding-right: 3px;
}
. PaginationNumbers a: active {
Color: #000;
Text-decoration: none;
Border: #999 1px solid; background-color: # F0F0F0; padding-left: 3px; padding-right: 3px;
}
-->
</Style>
</Head>
<Body>
<Div style = "margin-left: 64px; margin-right: 64px;">
<H2> Total Items: <? Php echo $ nr;?> </H2>
</Div>
<Div style = "margin-left: 58px; margin-right: 58px; padding: 6px; background-color: # FFF; border: #999 1px solid;"> <? Php echo $ paginationDisplay;?> </Div>
<Div style = "margin-left: 64px; margin-right: 64px;"> <? Php print "$ outputList";?> </Div>
<Div style = "margin-left: 58px; margin-right: 58px; padding: 6px; background-color: # FFF; border: #999 1px solid;"> <? Php echo $ paginationDisplay;?> </Div>
</Body>
</Html> |
Effect
Page
6Of 39 Back 4 5 6 7 8 Next