Page paging Function

Source: Internet
Author: User

Provides an instance classification function. If you need it, you can refer to it.

<? Php tutorial
// 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 tutorial _ connect ("localhost", "root", "root") die ('database tutorial 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;
?>

The following figure shows the database structure.

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

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.