PHP and MySQL pagination display instance analysis

Source: Internet
Author: User
Tags php and php and mysql prev mysql database
Web development is the mainstream of distributed programming development in the future, the common web development involves dealing with the database, the client reads from the server side is usually in the form of pagination to display, one page of reading up is both convenient and beautiful. So the write paging program is an important part of web development, here, we together to study the preparation of the paging program.

   the principle of the paging procedure

The paging program has two very important parameters: each page displays several records ($pagesize) and is currently the first page ($page). With these two parameters can be very convenient to write the paging program, we use the MySQL database as a data source, in MySQL if you want to remove a section of the table specific content can use the T-SQL statement: SELECT * FROM table limit offset,rows to implement. The offset here is the record deviation, which is calculated by offset= $pagesize * ($page-1), rows is the number of records to display, and this is $page. That is, select * FROM table limit 10,10 The meaning of this statement is to remove 20 records starting from the 11th record.

   Second, the main code analysis

$pagesize = 10; Set the number of records to display per page
$conn =mysql_connect ("localhost", "root", ""); Connecting to a database
$rs =mysql_query ("SELECT count (*) from tb_product", $conn); Get the total number of records $rs
$myrow = Mysql_fetch_array ($RS);
$numrows = $myrow [0];

Calculate Total Pages

$pages =intval ($numrows/$pagesize);

Determine page setting

if (Isset ($_get[' page ')) {
$page =intval ($_get[' page '));
}
else{
$page = 1; Otherwise, set to the first page
}


   Third, create use cases with table MyTable

CREATE TABLE myTable (id int not NULL auto_increment,news_title varchar (), News_cont text,add_time datetime,primary KEY ( ID))


   Four, complete code

<title> Sample PHP pagination </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 on each 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);
Read the specified number of records
$rs =mysql_query ("SELECT * from myTable" 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 '". $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 the Windows2000 server+php4.4.0+mysql5.0.16. The example shows the paging format as [1][2][3] ... Such a form. If you want to display "first prev next last" form, please add the following code:

$first = 1;
$prev = $page-1;
$next = $page +1;
$last = $pages;

if ($page > 1)
{
echo " homepage ";
echo " previous page ";
}

if ($page $pages)
{
echo " next page
echo " last ";
}


In fact, the write pagination display code is very simple, as long as mastered its working principle. Hopefully this article will be able to bring to those who need this program Web Programmer's Help.

Related Article

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.