Prepare data:
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 ):Copy codeThe Code is as follows: <? Php
// 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. = "<a href = '? Page = 1'> homepage </a> ";
$ Pagenav. = "<a href = '? Page = $ pre_page '> previous page </a> ";
$ Pagenav. = "<a href = '? Page = $ next_page '> next page </a> ";
$ Pagenav. = "<a href = '? Page = $ page_count '> last page </a> ";
$ Pagenav. = "Jump to <select name = 'topage' size = '1' onchange = 'window. location = \"? Page = \ "+ this. value'> \ n ";
For ($ I = 1; $ I <= $ page_count; $ I ++ ){
If ($ I = $ page) $ pagenav. = "<option value = '$ I' selected> $ I </option> \ n ";
Else $ pagenav. = "<option value = '$ I'> $ I </option> \ n ";
}
} // Page Function
// Example
If (! $ 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 Function
I wrote a simple paging function in php, and the database call is also written in it. Users can delete it themselves!Copy codeThe 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 '<dl> <dt> q :'. $ v ["question"]. '</dt> <dd> :'. $ v ["ansower"]. date ("Y-m-d H: I: s", $ v ["postTime"]). '</dd> </dl> ';
}
$ Pagebar. = "<div class = \" page \ "> ";
$ Pagebar. = "<ol> ";
If ($ page_current! = 1 ){
$ Pagebar. = '<li> <a href = "javascript: get_comment (1)" class = "sx"> FIRST </a> </li> ';
}
For ($ I = $ page_start; $ I <= $ page_end; $ I ++ ){
If ($ I = $ page_current ){
$ Pagebar. = "<li> <span class = \" sort \ ">". $ I. "</span> </li> ";
} Else {
$ Pagebar. = "<li> <a href = 'javascript: get_comment (". $ I. ") '> ". $ I. "</a> </li> ";
}
}
If ($ page_current! = $ Page_count ){
$ Pagebar. = "<li> <a href = 'javascript: get_comment (". $ page_count. ") 'class = 'sx '> END </a> </li> ";
}
$ Pagebar. = "</ol> ";
$ Pagebar. = "</div> ";
Echo $ pagebar;
}