Principle analysis of php+mysql paging query code
- WBOYOriginal
- 2016-07-25 08:52:151056browse
-
- $pagesize=10; //Set the number of records displayed on each page
- $conn=mysql_connect("localhost","root",""); //Connect to the 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 the total number of pages
- $pages= intval($numrows/$pagesize);
- //Determine the page number setting
- if (isset($_GET['page'])){
- $page=intval($_GET['page']);
- }
- else {
- $page=1; // Otherwise, set to the first page
- }
Copy code
3. Create the use case table myTable
-
- php pagination - bbs.it-home.org
-
- $conn=mysql_connect("localhost","root","");
- //Set each The number of records displayed on one page
- $pagesize=1;
- mysql_select_db("mydata",$conn);
- //Get the total number of records $rs, and calculate the total number of pages using
- $rs=mysql_query("select count(*) from tb_product",$conn);
- $myrow = mysql_fetch_array($rs);
- $numrows=$myrow[0];
- //Calculate the total number of pages
- $pages=intval($numrows/$pagesize);
- if ( $numrows%$pagesize)
- $pages++;
- //Set the number of pages
- if (isset($_GET['page'])){
- $page=intval($_GET['page']);
- }
- else {
- //Set as the first page
- $page=1;
- }
- //Calculate the record offset
- $offset=$pagesize*($page - 1);
- //Read the specified number of records
- $rs =mysql_query("select * from myTable order by id desc limit $offset,$pagesize",$conn);
- if ($myrow = mysql_fetch_array($rs))
- {
- $i=0;
- ?>
- < ;table border="0" width="80%">
-
-
-
Title |
-
-
Release time |
-
- < ?php
- do {
- $i++;
- ?>
-
-
=$myrow["news_title"]?> |
-
=$myrow["news_cont"]?> |
-
- }
- while ($myrow = mysql_fetch_array ($rs));
- echo "
|
";
}
echo "There are ".$pages." pages (".$page."/".$ pages.")";
for ($i=1;$i< $page;$i++) echo "[". $i ."] ";
echo "[".$page."]";
for ($i=$page+1;$i<=$pages;$i++) echo "< ;a href='fenye.php?page=".$i."'>[".$i ."] ";
echo " ";
?>
Copy code
5. Summary
The code runs normally on windows2003 server+php4.4.0+mysql5.0.16.
The paging format shown in this example is [1][2][3]….
If you need to display it in the form of "Home Page Previous Page Next Page Last Page", the code:
-
- $first=1;
- $prev=$page-1;
- $next=$page+1;
- $last=$pages;
- if ($page > 1)
- {
- echo " Homepage ";
- echo "Previous page ";
- }
- if ($page < $pages)
- {
- echo "
- echo "Last page ";
- }
Copy code
The above paging code is relatively simple. It can be used as a reference for getting started. Once you have mastered the principles of PHP paging, it will be much easier to write paging code.
Recommended reading:
- php and ajax no refresh paging code
- php article paging implementation code
- php limit page turning (pagination) code
- PHP paging class with multiple paging methods
- PHP pagination code for previous page and next page
- PHP paging code for the first ten pages and the next ten pages
- Example of simple php pagination code
- A good php pagination code
- A paging function: Previous page Next page
- A useful php paging class
- php long article pagination code
- A practical php paging class
- Fast php paging class
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn