A simple paging program for php and mysql data. If you have a simple php and mysql data paging program, please refer to it. The code is as follows: Copy the code? PhpAdamsCustomPHPMySQLPaginationTutorialandScriptYouhaveto a simple php and mysql data paging program for your reference.
| The code is as follows: |
|
|
// 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. = ''. $ pn .'';
$ CenterPages. = ''. $ add1 .'';
} Else if ($ pn ==$ lastPage ){
$ CenterPages. = ''. $ sub1 .'';
$ CenterPages. = ''. $ pn .'';
} Else if ($ pn> 2 & $ pn <($ lastPage-1 )){
$ CenterPages. = ''. $ sub2 .'';
$ CenterPages. = ''. $ sub1 .'';
$ CenterPages. = ''. $ pn .'';
$ CenterPages. = ''. $ add1 .'';
$ CenterPages. = ''. $ add2 .'';
} Else if ($ pn> 1 & $ pn <$ lastPage ){
$ CenterPages. = ''. $ sub1 .'';
$ CenterPages. = ''. $ pn .'';
$ CenterPages. = ''. $ add1 .'';
}
// 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'. $ Pn .'Of '. $ lastPage .'';
// If we are not on page 1 we can place the Back button
If ($ pn! = 1 ){
$ Previous = $ pn-1;
$ PaginationDisplay. = 'back ';
}
// Lay in the clickable numbers display here between the Back and Next links
$ PaginationDisplay. = ''. $ centerPages .'';
// If we are not on the very last page we can place the Next button
If ($ pn! = $ LastPage ){
$ NextPage = $ pn + 1;
$ PaginationDisplay. = 'next ';
}
}
/// // 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. = ''. $ firstname.''. $ country .' ';
} // Close while loop
?>
Adam's Pagination
Total Items:
|
Effect
Page
6Of 39 Back 4 5 6 7 8 Next
Bytes. The code is as follows? Php // Adam's Custom PHP MySQL Pagination Tutorial and Script // You have...