Php paging code instance with comments
- Header ("content-type: text/html; charset = utf-8 ");
- // Database connection
- $ Conn = mysql_connect ("localhost", "root", "111") or die ("not connnected:". mysql_error ());
- Mysql_select_db ("test", $ conn );
- Mysql_query ("set names utf8 ");
- // Query the total number of rows of data
- $ Sql1 = "select count (*) from user ";
- $ Ret1 = mysql_query ($ sql1 );
- $ Row1 = mysql_fetch_row ($ ret1 );
- $ Tot = $ row1 [0];
- // How many rows of data per page
- $ Length = 5;
- // Total number of pages
- $ Totpage = ceil ($ tot/$ length );
- // Current page number
- $ Page = @ $ _ GET ['P']? $ _ GET ['P']: 1;
- // Lower limit of limit
- $ Offset = ($ page-1) * $ length;
- Echo" ";
- Echo "php padding ";
- Echo"
- Echo"
- Echo"
- Echo"
- Echo"
- Echo"
- // Display the queried data in a table
- Echo"
- Echo"
- }
- $ Sql2 = "select * from user order by id limit {$ offset}, {$ length }";
- $ Ret2 = mysql_query ($ sql2 );
- While ($ row2 = mysql_fetch_assoc ($ ret2 )){
- Echo"
- Echo"
";
";
| ID | ";
USER | ";
PASS | ";
";
";
| {$ Row2 ['id']} |
{$ Row2 ['name']} |
{$ Row2 ['pass']} | ";
";
";
- // Previous and next pages
- $ Prevpage = $ page-1;
- If ($ page >=$ totpage ){
- $ Nextpage = $ totpage;
- } Else {
- $ Nextpage = $ page + 1;
- }
- // Jump
- Echo "previous page | next page ";
- Echo"
";
Key points of paging code: <1> "$ sql2 =" select * from user order by id limit {$ offset}, {$ length }";", the relationship between $ offset, $ length, and the number of pages. <2> obtain the previous and next pages, as well as the critical point. |