The following small series will bring you an article on PHP paging a simple implementation of the simplest PHP paging code. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at the PHP paging code that must be used in various program development processes. it is also 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'
'. $ V ['goods _ id']. ','. $ v ['goods _ name']. '--- ¥ '. $ v ['shop _ price'].'';}
// Display the paginated number list for ($ I = 1; $ I <= $ totalpage; $ I ++) {echo ''. $ I .'';}
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.
The above is a small Editor for you to bring PHP paging to explore a simple PHP paging code implementation of all the content, I hope you like it ~
If you want to learn more about game activities and strategies, please stay tuned to this site. This site will bring you the best viewing, the most fun, and the most fresh game information in the first place. For more exciting content, go to the php game Channel!