PHP paging function code (simple and practical ). Prepare Data: create a database named test and execute the following statement (create a table named test: id, sex, and name) CREATETABLE 'test' ('id' INT (4) prepare Data for NOTNULLAUTO_INCREMEN:
Create a database named test
Execute the following statement (create a table named test: id, sex, and name)
Create table 'test '(
'Id' INT (4) not null AUTO_INCREMENT primary key,
'Sex 'INT (1) not null,
'Name' VARCHAR (20) not null) ENGINE = myisam character set utf8 COLLATE utf8_bin;
Add data to the test table, for example:
1 Xiaoqiang
2 0 Red
3 0 Xiaoli
4 1 soldier
5, 1, 3
6 0 Li Si
7 0 Wu Xin
Write the PHP statement (index. php ):
The code is as follows:
// Page function
$ Page = $ _ GET ["page"];
Function Page ($ rows, $ page_size ){
Global $ page, $ select_from, $ select_limit, $ pagenav;
$ Page_count = ceil ($ rows/$ page_size );
If ($ page <= 1 | $ page = '') $ page = 1;
If ($ page >=$ page_count) $ page = $ page_count;
$ Select_limit = $ page_size;
$ Select_from = ($ page-1) * $ page_size .',';
$ Pre_page = ($ page = 1 )? 1: $ page-1;
$ Next_page = ($ page = $ page_count )? $ Page_count: $ page + 1;
$ Pagenav. = "$ page/$ page_count page $ rows records ";
$ Pagenav. = "homepage ";
$ Pagenav. = "previous page ";
$ Pagenav. = "next page ";
$ Pagenav. = "Last page ";
$ Pagenav. = "Jump\ N ";For ($ I = 1; $ I <= $ page_count; $ I ++ ){If ($ I = $ page) $ pagenav. ="$ I\ N ";Else $ pagenav. ="$ I\ N ";}} // Page function// ExampleIf (! $ Conn = mysql_connect ("localhost", "root", "root") die ('database selection failed! ');If (! Mysql_select_db ("test", $ conn) die ('database selection failed! ');Mysql_query ('set names gbk ');// Use the Page function to calculate the records from which $ select_from is retrieved and the pagenav output paging navigation.$ Rows = mysql_num_rows (mysql_query ("select * from test "));Page ($ rows, 2 );$ SQL = "select * from test limit $ select_from $ select_limit ";$ Rst = mysql_query ($ SQL );While ($ row = mysql_fetch_array ($ rst )){Echo "$ row [id]-$ row [sex]-$ row [name]";}Echo $ pagenav;?> Browse the index. php page,It should be byebye. it's really easy to use!PHP simple paging functionI wrote a simple paging function in php, and the database call is also written in it. Users can delete it themselves! The code is as follows: Function getask (){$ SQL = "select * from cms_ask where ansower <> ''";// Change the method here$ Q_sq = mysql_query ($ SQL );$ Count = mysql_num_rows ($ q_sq );$ Page_size = 8;$ Page_current = isset ($ GLOBALS ['Page'])? Intval ($ GLOBALS ['Page']): 1;$ Page_count = ceil ($ count/$ page_size );$ Page_start = $ page_current-4;$ Page_end = $ page_current + 4;If ($ page_current <5 ){$ Page_start = 1;$ Page_end = 5;}If ($ page_current> $ page_count-4 ){$ Page_start = $ page_count-8;$ Page_end = $ page_count;}If ($ page_start <1)$ Page_start = 1;If ($ page_end> $ page_count)$ Page_end = $ page_count;$ Pagebar = "";$ SQL = "select * from cms_ask where ansower <> ''Order by id desc limit". ($ page_current-1) * $ page_size). ",". $ page_size;$ Row = $ this-> user-> getall ("$ SQL ");Foreach ($ row as $ v ){Echo'Q: '. $ v ["question"].'A: '. $ v ["ansower"]. date ("Y-m-d H: I: s", $ v ["postTime"]).'';}$ Pagebar. ="";$ Pagebar. ="";If ($ page_current! = 1 ){$ Pagebar. ='FIRST';}For ($ I = $ page_start; $ I <= $ page_end; $ I ++ ){If ($ I = $ page_current ){$ Pagebar. ="". $ I ."";} Else {$ Pagebar. ="". $ I ."";}}If ($ page_current! = $ Page_count ){$ Pagebar. ="END";}$ Pagebar. ="";$ Pagebar. ="";Echo $ pagebar;}CREATE a database named test and execute the following statement (CREATE a TABLE named test: id, sex, and name) create table 'test' ('id' INT (4) not null AUTO_INCREMEN...