pagination | In the PHP network database programming, it is unavoidable to take into account the results of database records display problem, in order to render beautiful pages and speed up the loading speed of the page, the database records need to be paged out.
and PHP is the perfect combination of MySQL database records of the pagination display example to share with you.
The MySQL database is Xinxiku and the data table is joke. The SQL statement defines the following:
CREATE TABLE Joke ( ID int (5) not NULL auto_increment, Biaoti varchar () not NULL, Neirong Text Not NULL, PRIMARY KEY (ID) ); Field Description:
ID: Record number, auto increment and primary key
Biaoti: Title
Neirong: Content
<title> pagination Display Implementation method </title> <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "> <script language= "JavaScript" > /* Define a pop-up window to display the specific content * * function Popwin (URL) { window.open (URL, "", "left=340, top=190, height=280, width=400, Resizable=yes, Scrollbars=yes, Status=no, Toolbar=no, Menubar=no, Location=no "); } </script>
<body leftmargin=0 topmargin=0 rightmargin=0 >
<?php Connecting to a database $db =mysql_connect ("localhost", "root", ""); mysql_select_db ("Xinxiku", $db); Set the number of records to display on each page $pagesize = 15; Get the total number of records, calculate total pages with $res =mysql_query ("SELECT count (*) from joke", $db); $myrow = Mysql_fetch_array ($res); $numrows = $myrow [0]; Calculate Total Pages $pages =intval ($numrows/$pagesize); if ($numrows% $pagesize) $pages + +; Determine whether the page is set or not, such as no definition of the homepage if (!isset ($page)) $page = 1; Judge go to Page if (Isset ($ys)) if ($ys > $pages) $page = $pages; Else $page = $ys; Calculate record offset $offset = $pagesize * ($page-1); Take a record $res =mysql_query ("Select Id,biaoti from joke ORDER by id desc limit $offset, $pagesize", $db); Loop display of records if ($myrow = mysql_fetch_array ($res)) { $i = 0; ?> <table width= "101%" border= "0" cellspacing= "0" cellpadding= "0" > <tr> <TD width= "5%" bgcolor= "#E1E9FB" ></td> <TD width= "bgcolor=" "#E1E9FB" ><font color= "#FF6666" size= "2" > Content </font></td> </tr> <?php do { $i + +; ?> <tr> <TD width= "5%" bgcolor= "#E6F2FF" ><?php echo $i;? ></td> <TD width= "bgcolor=" "#E6F2FF" ><font size= "2" > <a href= "Javascript:popwin" (' jokenr.php?id=<?php echo $myrow [0];? > ') "><?php echo $myrow [1];? ></a></font></td> </tr> <?php } while ($myrow = Mysql_fetch_array ($res)); echo "</table>"; } Show Total Pages echo "<div align= ' center ' > common". $pages. " Page (". $page." /". $pages.") <br> "; Show pagination for ($i =1; $i < $page; $i + +) echo "<a href= ' fy.php?page=". $i. "' > ". $i." Page </a> "; echo "section" $page. " Page "; for ($i = $page +1; $i <= $pages; $i + +) echo "<a href= ' fy.php?page=". $i. "' > ". $i." Page </a> ";
echo "<br>"; Show go to Page echo "<form action= ' fy.php ' method= ' post ' >"; Calculates the page value of the first page, previous, Next, and last pages $first = 1; $prev = $page-1; $next = $page +1; $last = $pages; if ($page >1) { echo "<a href= ' fy.php?page=". $first. "' > Home </a> "; echo "<a href= ' fy.php?page=". $prev. "' > Prev </a> "; } if ($page < $pages) { echo "<a href= ' fy.php?page=". $next. "' > next page </a> "; echo "<a href= ' fy.php?page=". $last. "' > Last </a> "; } echo "Go to <input type=text name= ' ys ' size= ' 2 ' value=". $page. " > page "; echo "<input type=submit name= ' submit ' value= ' Go ' >"; echo "</form>"; echo "</div>";
?> </body> Program Description:
1. Variable description
Variable $page: Stores the number of pages currently displayed.
Variable $pages: The total number of pages stored.
Variable $offset: The offset that holds the current number of pages.
Variable $pagesize: Holds the number of records displayed on each page.
2. Description of MySQL statement
Select Id,biaoti from joke the ORDER by id desc limit $offset, $pagesize The limit clause is used to limit the number of rows returned by the SELECT statement. The first parameter in limit specifies the offset of the first row to be returned, and the second parameter specifies the maximum number of rows to return pagesize.
Implementation principle:
Pass a page variable page in the program, calculate the initial record position offset based on the page values passed, and pagesize the recordset according to the number of records required per page. And then the show.
Program Implementation function:
The program provides two ways to display the page (assuming the current page is the third page).
1, all the pages are displayed, according to the number of pages to provide links, the current page does not provide links. This is suitable for pages that are not too much of a situation. The style is shown in Figure 1:
Http://www.ccidnet.com/html/tech/web/2001/05/22/image/1.jpg
2, provide the total number of pages, page through the provision of the first page, Prev, Next, last and jump arrival to achieve. Of course, if the current page is for each page, do not provide the first page, the previous link, the current page is the last page, do not provide the next page, the last link. The style is shown in Figure 2:
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.