Php paging Functions

Source: Internet
Author: User

<? Php
/*
Requirement: Create a test database and create a test table in it.
You only need to enter the id field ..
Due to the limited level, errors are inevitable ..
*/
$ 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 );

?>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> paging example </title>
<Script language = "JavaScript" type = "text/JavaScript">
<! --
Function MM_jumpMenu (targ, selObj, restore) {// v3.0
Eval (targ + ". location = '" + selObj. options [selObj. selectedIndex]. value + "'");
If (restore) selObj. selectedIndex = 0;
}
// -->
</Script>
<Style type = "text/css">
A {text-decoration: none ;}
A: hover {text-decoration: underline}
Table {font-size: 12px ;}
. Tb {background-color: #73BB95}
. Tr {background-color: # FFFFFF}
</Style>
</Head>

<Body>
<Table width = "30%" border = "0" align = "center" cellpadding = "0" cellspacing = "1" class = "tb">
<Tr>
<Td height = "24"> <div align = "left"> paging example </div> </td>
</Tr>
<? Php if ($ totalRows1) {// the record set is not empty.
Do {
?>
<Tr class = "tr">
<Td height = "24"> <div align = "center"> <? Php echo $ row ['id'];?> </Div> </td>
</Tr>
<? Php} while ($ row = mysql_fetch_assoc ($ result);?>
</Table>

<Table width = "95%" border = "0" align = "center" cellpadding = "0" cellspacing = "0">
<Tr> <form name = "form1">
<Td height = "27"> <div align = "center">
<? Php
Echo "A total of <font color = \" # ff0000 \ "> $ totalRows1 </font> records ";
Echo "<font color = \" # ff0000 \ ">". $ page. "</font>". "/". $ totalpages. "page ";

// Implement <1 2 3 4 5> paging links
$ Pre = $ page-1; // Previous page
$ Next = $ page + 1; // next page
$ Maxpages = 4; // when processing pages <1 2 3 4> display 4 pages
$ Pagepre = 1; // if the current page is 4, the first $ pagepre page is displayed, as shown in <3/4/5 6>.

If ($ page! = 1) {echo "<a href = '". $ _ SERVER ['php _ SELF']. "'> </a> ";
Echo "<a href = '". $ _ SERVER ['php _ SELF']. '? Page = '. $ pre. "'> </a> ";}

If ($ maxpages >=$ totalpages) // if the total number of records is insufficient, four pages are displayed.
{$ Pgstart = 1; $ pgend = $ totalpages;} // you can print out a different page.
Elseif ($ page-$ pagepre-1 + $ maxpages)> $ totalpages) // as if the total number of pages is 6 and the current number is 5, the previous 3 4 is displayed, not just 4
{$ Pgstart = $ totalpages-$ maxpages + 1; $ pgend = $ totalpages ;}
Else {
$ Pgstart = ($ page <= $ pagepre )? 1 :( $ page-$ pagepre); // when the current page is 1, it will only be 1 2 3 4 >>> not 0 1 2 3 >>>
$ Pgend = ($ pgstart = 1 )? $ Maxpages :( $ pgstart + $ maxpages-1 ));
}

For ($ pg = $ pgstart; $ pg <= $ pgend; $ pg ++) {// jump menu
If ($ pg = $ page) echo "<a href = \" ". $ _ SERVER ['php _ SELF ']."? Page = $ pg \ "> <font color = \" # ff0000 \ "> $ pg </font> </a> ";
Else echo "<a href = \" ". $ _ SERVER ['php _ SELF ']."? Page = $ pg \ "> $ pg </a> ";
}
If ($ page! = $ Totalpages)
{Echo "<a href = '". $ _ SERVER ['php _ SELF']. '? Page = '. $ next. "' >></a> ";
Echo "<a href = '". $ _ SERVER ['php _ SELF']. '? Page = '. $ totalpages. "' >>></a> ";}
?>
<Select name = "menu1" onChange = "MM_jumpMenu ('parent', this, 0)">
<Option value = ""> select </option>
<? Php for ($ pg1 = 1; $ pg1 <= $ totalpages; $ pg1 ++ ){
Echo "<option value = \" ". $ _ SERVER ['php _ SELF ']."? Page = $ pg1 \ ">". $ pg1. "</option> ";
}?>
</Select>
</Td> </form>
</Tr>
</Table>
<? Php} else {// when the record set is empty?>
<Tr class = "tr">
<Td height = "24"> <div align = "center"> no records </div> </td>
</Tr>
</Table>
<? Php }?>
</Body>
</Html>
<? Php
Mysql_free_result ($ result1 );
Mysql_free_result ($ result );
?>

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.