Sample analysis of PHP MySQL pagination display in web development

Source: Internet
Author: User
Tags php mysql 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



php pagination Example



$conn =mysql_connect (" localhost "," root "," ");
//Set the number of records displayed on each page
$pagesize = 1;
mysql_select_db ("MyData", $conn);
//Get total number of records $rs, calculate total pages by
$rs =mysql_query ("SELECT count (*) from tb_product", $conn);
$myrow = Mysql_fetch_array ($ RS);
$numrows = $myrow [0];
//Count total pages

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.