In the development of PHP framework is more and more widely used, the framework with more than the original not practice gradually will be the past, I hope you do not give the basic things to the past Oh! In this article we are talking about PHP in conjunction with MySQL to achieve native paging, and now the general framework encapsulates the paging class, do not need to write manually. But when you have nothing to do, you can try to write, exercise your ability to write code.
Php+mysql paging is primarily implemented based on the limit in SQL statements.
Limit statement:
SELECT * FROM table limit ' limit ', ' offset ';
You can view the limit as the number of pages in a page, offset as a bar per page.
Achieve results
Here is just an example code, the main implementation of paging function, page effects and other related not intentionally debugging.
Main implementation features are: Home, last, previous page, next page, statistics total pages, positioning the current page, digital display, the maximum of four, the rest of ... Said.
Specific code implementation:
Here is the database with PDO operations
<?phpheader ("Content-type:text/html;charset=utf-8");//1, incoming page number $page = $_get[' P '];//2, extract data based on page number $pdo = new PDO (' MySQL : Host=localhost;dbname=ceshi ', ' root ', ' root '); $pdo->setattribute (pdo::attr_emulate_prepares,false); $sql = ' SELECT * from ' goods ' limit:start,:offset '; $limit = ($page-1); $offset = 3; $stmt = $pdo->prepare ($sql); $sqlnum = ' SEL ECT COUNT (*) from ' goods '; $stmtnum = $pdo->prepare ($sqlnum); $stmtnum->execute (); $num = $stmtnum->fetch (PDO: : FETCH_ASSOC); $stmt->bindparam (': Start ', $limit); $stmt->bindparam (': Offset ', $offset); if (! $stmt, Execute ()) {throw new Exception (' Find failed ', 1); } $data = $stmt->fetchall (PDO::FETCH_ASSOC); Var_dump ($data);//3, display data + show paging bar/** total pages **/$end = Floor ($num [' COUNT (*) '] /$offset) +1; $page _banner = ' <a href= "'. $_server[' php_self ']. '? P=1 "> Home </a>", if ($page <=1) {$page _banner. = ' <a style= ' color: #B8BBB3; " > prev </a> ';} else{$page _banner. = ' <a href= ' '. $_server[' php_self '. '? P= '. ($page-1). ' " > prev </a> ';} if ($page +3>4) {$page _banner. = ' ... ';} if ($page +4) >= $end) {$page _banner. = ' <a href= "'. $_server[' php_self ']. '? P= '. ($end-3). ' " > '. ($end-3). ' </a> '; $page _banner. = ' <a href= "'. $_server[' php_self ']. '? P= '. ($end-2). ' " > '. ($end-2). ' </a> '; $page _banner. = ' <a href= "'. $_server[' php_self ']. '? P= '. ($end-1). ' " > '. ($end-1). ' </a> '; $page _banner. = ' <a href= "'. $_server[' php_self ']. '? P= '. ($end). ' " > '. ($end). ' </a> ';} else{for ($i =0; $i <4; $i + +) {$page _banner. = ' <a href= ' '. $_server[' php_self ']. '? P= '. ($page + $i). ' " > '. ($page + $i). ' </a> '; }}if (($page +4) < $end) {$page _banner. = ' ... ';} if ($page <= $num [' COUNT (*) ']/$offset) {$page _banner. = ' <a href= ' '. $_server[' Php_Self ']. P= '. ($page + 1). ' " > next </a> ';} else{$page _banner. = ' <a style= ' color: #B8BBB3; " > next </a> ';} $page _banner. = ' <a href= "'. $_server[' php_self ']. '? P= '. $end. ' " > end </a> '; $page _banner. = ' <span> & nbsp; total '. $end. ' Page </span> current <span> ' $page. ' Page </span> "Echo $page _banner;? >
You can practice in your own local practice Oh! Do not understand more to practice more! These native-based things forget that it's hard to pick them up at the end!!
Related recommendations:
Implementation of the PHP paging class
Today to share with you a good-looking PHP page Class!
The implementation principle of PHP paging
Today will take you to explain the principle of PHP paging.
A universal PHP Page class instance code
This article mainly introduces the universal PHP paging class, especially useful, need to use PHP paging class friends do not miss.
thinkphp Paging Implementation Effect
A large amount of data will need to display the content of the page, this article is to introduce thinkphp pagination to organize, have the need for friends to understand.