PHP with MySQL for native paging

Source: Internet
Author: User
Tags php framework stmt
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 ">&nbsp;&nbsp;&nbsp; Home </a>", if ($page <=1) {$page _banner. = ' <a style= ' color: #B8BBB3; " >&nbsp;&nbsp;&nbsp; prev </a> ';} else{$page _banner. = ' <a href= ' '. $_server[' php_self '. '? P= '. ($page-1). ' " >&nbsp;&nbsp;&nbsp; prev </a> ';} if ($page +3>4) {$page _banner. = ' ... ';} if ($page +4) >= $end) {$page _banner. = ' <a href= "'. $_server[' php_self ']. '? P= '. ($end-3). ' " >&nbsp;&nbsp;&nbsp; '. ($end-3). '    </a> '; $page _banner. = ' <a href= "'. $_server[' php_self ']. '? P= '. ($end-2). ' " >&nbsp;&nbsp;&nbsp; '. ($end-2). '    </a> '; $page _banner. = ' <a href= "'. $_server[' php_self ']. '? P= '. ($end-1). ' " >&nbsp;&nbsp;&nbsp; '. ($end-1). '    </a> '; $page _banner. = ' <a href= "'. $_server[' php_self ']. '? P= '. ($end). ' " >&nbsp;&nbsp;&nbsp; '. ($end). ' </a> ';} else{for ($i =0; $i <4; $i + +) {$page _banner. = ' <a href= ' '. $_server[' php_self ']. '? P= '. ($page + $i). ' " >&nbsp;&nbsp;&nbsp; '. ($page + $i). '    </a> '; }}if (($page +4) < $end) {$page _banner. = ' ... ';} if ($page <= $num [' COUNT (*) ']/$offset) {$page _banner. = ' <a href= ' '. $_server[' Php_Self ']. P= '. ($page + 1). ' " >&nbsp;&nbsp;&nbsp;&nbsp; next </a> ';} else{$page _banner. = ' <a style= ' color: #B8BBB3; " >&nbsp;&nbsp;&nbsp; next </a> ';} $page _banner. = ' <a href= "'. $_server[' php_self ']. '? P= '. $end. ' " >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end </a> '; $page _banner. = ' <span>&nbsp;& nbsp;&nbsp; total '. $end. ' Page </span>&nbsp;&nbsp;&nbsp; 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.

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.