Nice php + mysql paging class
- /*****************************
- * @ Class name: page
- * @ Parameter: $ myde_total-total number of records
- * $ Myde_size-number of records displayed on one page
- * $ Myde_page-current page
- * $ Myde_url-get the current url
- * @ Function: Paging implementation
- * @ Author: Song Haige
- */
- Class page {
- Private $ myde_total; // total number of records
- Private $ myde_size; // number of records displayed on one page
- Private $ myde_page; // current page
- Private $ myde_page_count; // total number of pages
- Private $ myde_ I; // Start Page
- Private $ myde_en; // The ending page number.
- Private $ myde_url; // Obtain the current url
- /*
- * $ Show_pages
- * Page display format. the page number of the link displayed is 2 * $ show_pages + 1.
- * For example, $ show_pages = 2, the page is displayed on the [Homepage] [Previous Page] 1 2 3 4 5 [Next Page] [last page]
- */
- Private $ show_pages;
- Public function _ construct ($ myde_total = 1, $ myde_size = 1, $ myde_page = 1, $ myde_url, $ show_pages = 2 ){
- $ This-> myde_total = $ this-> numeric ($ myde_total );
- $ This-> myde_size = $ this-> numeric ($ myde_size );
- $ This-> myde_page = $ this-> numeric ($ myde_page );
- $ This-> myde_page_count = ceil ($ this-> myde_total/$ this-> myde_size );
- $ This-> myde_url = $ myde_url;
- If ($ this-> myde_total <0) $ this-> myde_total = 0;
- If ($ this-> myde_page <1) $ this-> myde_page = 1;
- If ($ this-> myde_page_count <1) $ this-> myde_page_count = 1;
- If ($ this-> myde_page> $ this-> myde_page_count) $ this-> myde_page = $ this-> myde_page_count;
- $ This-> limit = ($ this-> myde_page-1) * $ this-> myde_size;
- $ This-> myde_ I = $ this-> myde_page-$ show_pages;
- $ This-> myde_en = $ this-> myde_page + $ show_pages;
- If ($ this-> myde_ I <1 ){
- $ This-> myde_en = $ this-> myde_en + (1-$ this-> myde_ I );
- $ This-> myde_ I = 1;
- }
- If ($ this-> myde_en> $ this-> myde_page_count ){
- $ This-> myde_ I = $ this-> myde_ I-($ this-> myde_en-$ this-> myde_page_count );
- $ This-> myde_en = $ this-> myde_page_count;
- }
- If ($ this-> myde_ I <1) $ this-> myde_ I = 1;
- }
- // Check whether it is a number
- Private function numeric ($ num ){
- If (strlen ($ num )){
- If (! Preg_match ("/^ [0-9] + $/", $ num )){
- $ Num = 1;
- } Else {
- $ Num = substr ($ num, 0, 11 );
- }
- } Else {
- $ Num = 1;
- }
- Return $ num;
- }
- // Address replacement
- Private function page_replace ($ page ){
- Return str_replace ("{page}", $ page, $ this-> myde_url );
- }
- // Homepage
- Private function myde_home (){
- If ($ this-> myde_page! = 1 ){
- Return "page_replace (1)." "title =" homepage "> homepage ";
- } Else {
- Return"
Homepage ";
- }
- }
- // Previous Page
- Private function myde_prev (){
- If ($ this-> myde_page! = 1 ){
- Return "page_replace ($ this-> myde_page-1)." "title =" previous "> previous ";
- } Else {
- Return"
Previous Page ";
- }
- }
- // Next page
- Private function myde_next (){
- If ($ this-> myde_page! = $ This-> myde_page_count ){
- Return "page_replace ($ this-> myde_page + 1)." "title =" Next Page "> Next Page ";
- } Else {
- Return"
Next page ";
- }
- }
- // Last Page
- Private function myde_last (){
- If ($ this-> myde_page! = $ This-> myde_page_count ){
- Return "page_replace ($ this-> myde_page_count)." "title =" Last Page "> last page ";
- } Else {
- Return"
Last page ";
- }
- }
- // Output
- Public function myde_write ($ id = 'page '){
- $ Str ="
";
- $ Str. = $ this-> myde_home ();
- $ Str. = $ this-> myde_prev ();
- If ($ this-> myde_ I> 1 ){
- $ Str. ="
... ";
- }
- For ($ I = $ this-> myde_ I; $ I <= $ this-> myde_en; $ I ++ ){
- If ($ I ==$ this-> myde_page ){
- $ Str. = "page_replace ($ I)." "title =" no. ". $ I." page "class =" cur "> $ I ";
- } Else {
- $ Str. = "page_replace ($ I)." "title =" no. ". $ I." Page "> $ I ";
- }
- }
- If ($ this-> myde_en <$ this-> myde_page_count ){
- $ Str. ="
... ";
- }
- $ Str. = $ this-> myde_next ();
- $ Str. = $ this-> myde_last ();
- $ Str. ="
Total". $ This-> myde_page_count.
- "Page". $ This-> myde_total ."Data entries
";
- $ Str. ="
";
- Return $ str;
- }
- }
- ?>
Php + mysql paging principle: limit ($ curpage-1) * $ showrow, $ showrow
2. php display page code
- Require_once ('./page. class. php'); // paging class
- $ Showrow = 3; // number of lines displayed on one page
- $ Curpage = empty ($ _ GET ['Page'])? 1: $ _ GET ['Page']; // The current page should also handle non-numbers.
- $ Url = "? Page = {page} "; // page address. if the search condition is "? Page = {page} & q = ". $ _ GET ['Q']
- // The code for connecting to mysql is omitted. you can add the code for testing.
- $ SQL = "SELECT * FROM table ";
- $ Query = mysql_query ($ SQL );
- $ Total = mysql_num_rows ($ query); // The total number of records
- If (! Empty ($ _ GET ['Page']) & $ total! = 0 & $ curpage> ceil ($ total/$ showrow ))
- $ Curpage = ceil ($ total_rows/$ showrow); // The current page number is greater than the last page number.
- // Obtain data
- $ Get_data = "select * from table limit". ($ curpage-1) * $ showrow. ", $ showrow ;";
- ...
- ?>
- Simple and common PHP paging class -bbs.it-home.org
-
-
-
-
- If ($ total> $ showrow) {// The total number of records is greater than the number displayed on each page.
- $ Page = new page ($ total, $ showrow, $ curpage, $ url, 2 );
- Echo $ page-> myde_write ();
- }
- ?>
-
-
|