New Tricks-php paging Sample code
- /*
- Database test, a test table, unique field Id, used for testing
- @ Link http://bbs.it-home.org
- */
- $ Conn = mysql_connect ("localhost", "root ","");
- $ Maxnum = 2; // number of records per page
- Mysql_select_db ("test", $ conn );
- $ Query1 = "select count (*) AS totalrows FROM test ";
- $ Result1 = mysql_query ($ query1, $ conn) or die (mysql_error ());
- $ Row1 = mysql_fetch_assoc ($ result1 );
- $ TotalRows1 = $ row1 ['totalrows']; // The total number of data entries in the dataset.
- $ Totalpages = ceil ($ totalRows1/$ maxnum); // calculates the total number of pages that can be viewed. ceil () is the preceding function.
- If (! Isset ($ _ GET ['Page']) |! Intval ($ _ GET ['Page']) | $ _ GET ['Page']> $ totalpages) $ page = 1; // Default processing of three types of errors
- // When the url parameter page does not exist and the page number is not in decimal order, the default value is 1 when the page is larger than the number of split pages.
- Else $ page =$ _ GET ['Page'];
- $ Startnum = ($ page-1) * $ maxnum; // This parameter is obtained from entry $ startnum of the dataset. Note that the dataset starts from 0.
- $ Query = "SELECT * FROM test LIMIT $ startnum, $ maxnum"; // SELECT the $ maxnum row starting FROM $ startnum
- $ Result = mysql_query ($ query, $ conn) or die (mysql_error ());
- $ Row = mysql_fetch_assoc ($ result );
- ?>
- Paging example
|