Web development is the mainstream of distributed programming in the future, usually the web development involves dealing with the database, the client from the server-side reading is usually in the form of paging, page by page reading is both convenient and beautiful. So to write a paging program is an important part of web development, and here we work together to study the programming of the paging program.
first, the principle of the paging program
The paging program has two very important parameters: Each page shows several records ($pagesize) and the current page ($page). With these two parameters can be very convenient to write out the paging program, we use the MySQL database as a data source, in MySQL if you want to remove a particular piece of the table can be used in a certain content of T-SQL statement: SELECT * FROM table limit offset,rows to implement. Offset here is the record offset, it is calculated by offset= $pagesize * ($page-1), rows is the number of records to display, here is $page. That is to say, select * FROM table limit 10,10 This statement means to take out the table from the 11th record starting from the 20 records.
Second, the main code parsing
$pagesize=10; //设置每一页显示的记录数
$conn=mysql_connect("localhost","root",""); //连接数据库
$rs=mysql_query("select count(*) from tb_product",$conn); //取得记录总数$rs
$myrow = mysql_fetch_array($rs);
$numrows=$myrow[0];
//计算总页数
$pages=intval($numrows/$pagesize);
//判断页数设置
if (isset($_GET[‘page‘])){
$page=intval($_GET[‘page‘]);
}
else{
$page=1; //否则,设置为第一页
}
third, create use case table MyTable
create table myTable(id int NOT NULL auto_increment,news_title varchar(50),news_cont text,add_time datetime,PRIMARY KEY(id))
Iv. Complete Code
<title> PHP Pagination Example </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<body>
<?php
$conn =mysql_connect ("localhost", "root", "");
Set the number of records to display per page
$pagesize = 1;
mysql_select_db ("MyData", $conn);
Get the total number of records $rs, calculate total pages with
$rs =mysql_query ("SELECT count (*) from tb_product", $conn);
$myrow = Mysql_fetch_array ($RS);
$numrows = $myrow [0];
Calculate Total Pages
$pages =intval ($numrows/$pagesize);
if ($numrows% $pagesize)
$pages + +;
Set Number of pages
if (Isset ($_get[' page ')) {
$page =intval ($_get[' page ');
}
else{
Set as first page
$page = 1;
}
Calculate record offset
$offset = $pagesize * ($page-1);
Reads the specified number of records
$rs =mysql_query ("SELECT * from MyTable the ORDER by id desc limit $offset, $pagesize", $conn);
if ($myrow = mysql_fetch_array ($rs))
{
$i = 0;
? >
<table border= "0" width= "80%" >
<tr>
<TD width= "50%" bgcolor= "#E0E0E0" >
<p align= "center" > title </td>
<TD width= "50%" bgcolor= "#E0E0E0" >
<p align= "Center" > release time </td>
</tr>
<?php
do {
$i + +;
? >
<tr>
<TD width= "50%" ><?= $myrow ["News_title"]?> </td>
<TD width= "50%" ><?= $myrow ["News_cont"]?> </td>
</tr>
<?php
}
while ($myrow = Mysql_fetch_array ($rs));
echo "</table>";
}
echo "<div align= ' center ' > Total". $pages. " Page (". $page." /". $pages.") ";
for ($i =1; $i $page; $i + +)
echo "<a Href= ' fenye.php?page=". $i. "' >[". $i."] </a> ";
echo "[". $page. "]";
for ($i = $page +1; $i <= $pages; $i + +)
echo "<a Href= ' fenye.php?page=". $i. "' >[". $i."] </a> ";
echo "</div>";
? >
</body>
v. Summary
This example code runs normally on Windows2000 server+php4.4.0+mysql5.0.16. The page format shown by the example is [1][2][3] ... Such a form. If you want to display the "first page UP next page" In this form, please add the following code:
$first=1;
$prev=$page-1;
$next=$page+1;
$last=$pages;
if ($page > 1)
{
echo "<a href=‘fenye.php?page=".$first."‘>首页</a> ";
echo "<a href=‘fenye.php?page=".$prev."‘>上一页</a> ";
}
if ($page < $pages)
{
echo "<a href=‘fenye.php?page=".$next."‘>下一页</a>
echo "<a href=‘fenye.php?page=".$last."‘>尾页</a> ";
}
In fact, it is very simple to write pagination display code, as long as you know how it works. Hopefully this article will bring to the help of web programmers who need this program.
This is the following comment code
Php+mysql Page Display Example analysis