PHP paging: A simple PHP paging code implementation, a preliminary study of Paging

Source: Internet
Author: User

PHP paging: A simple PHP paging code implementation, a preliminary study of Paging

PHP paging code is required in various program development, and is required in website development.

To write paging code, you must first understand the SQL query statement: select * from goods limit 2, 7. The core of the PHP paging code is centered around this statement. SQL statement Description: query the goods data table to retrieve 7 data records starting from 2nd data records. In paging code, 7 indicates how many items are displayed on each page. 2. The formula is used to calculate the number of pages. Different data can be filtered out by inputting different parameters to replace the value of "2.

Index. php:

Include 'conn. php '; // introduce the database operation class $ conn = new conn (); // instantiate the database operation class $ total = $ conn-> getOne ('select count (*) as total from goods '); $ total = $ total ['Total']; // The total number of data records in the goods table $ num = 6; // number of lines displayed per page $ totalpage = ceil ($ total/$ num); // calculate the number of pages if (isset ($ _ GET ['page']) & $ _ GET ['page'] <= $ totalpage) {// a judgment is made here, if you get the data and the data size is smaller than the total number of pages, the current page parameter will be paid. Otherwise, you will be redirected to the first page $ thispage =$ _ GET ['page'];} else {$ thispage = 1 ;}
// Pay attention to the red part of the following SQL statement. The calculation is used to determine the data to be retrieved from, the current page is subtracted from 1 and then multiplied by the number of data entries per page. $ SQL = 'select goods_id, goods_name, shop_price from goods order by goods_id limit '. ($ thispage-1) * $ num. ','. $ num. ''; $ data = $ conn-> getAll ($ SQL); foreach ($ data as $ k => $ v) {echo '<li> '. $ v ['goods _ id']. ','. $ v ['goods _ name']. '--- ¥ '. $ v ['shop _ price']. '</li> ';}
// Display the paginated number list for ($ I = 1; $ I <= $ totalpage; $ I ++) {echo '<a href = "? Page = '. $ I.' "> '. $ I.' </a> ';}

The above code implements a simple PHP paging effect:

Only click to flip the page to display different flip data, which can be further improved. As long as the basic principles are understood, the subsequent work will be easier to develop.

 

Conn. php code:

/** Connect to the database for related query operations */class conn {public function _ construct () {include_once ('config. php '); try {$ this-> pdo = new PDO ('mysql: host = localhost; dbname = test', 'root', '123 '); $ this-> pdo-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); $ this-> pdo-> exec ('set names utf8 ');} catch (PDOException $ e) {echo 'database Connection Failed :'. $ e-> getMessage (); exit () ;}// get a row of data public function getOne ($ SQL) {$ rs = $ this-> pdo-> query ($ SQL)-> fetch (PDO: FETCH_ASSOC); return $ rs ;} // obtain multi-row data results public function getAll ($ SQL) {$ rs = $ this-> pdo-> query ($ SQL)-> fetchall (PDO: FETCH_ASSOC ); return $ rs ;}}

The conn. php function is used to connect to the database and retrieve data. Here I use pdo and organize code according to your habits.

Config. php:

/** Configure database information */$ pai_dbhost = 'localhost'; $ pai_dbname = 'test'; $ pai_dbuser = 'root'; $ pai_dbpw = '2016 ';

  

This example is only used to describe the basic paging principle. There are still many modifications to this example.

Sample Code download: http://www.superphp.cn/yuanma/332.html

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.