PHP paging: a simple implementation of the PHP paging code.

Source: Internet
Author: User
PHP paging: a simple implementation of PHP paging code and a preliminary study of paging. PHP paging: a simple implementation of PHP paging code. Paging: PHP paging code is required in various program development processes, in website development, it is a required PHP page. first, a simple implementation of the simplest PHP page code.

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'
  • '. $ 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 jb51 game Channel!

    Refer is a simple implementation of the most simple PHP paging code. a preliminary study of paging PHP paging code is required in various program development. it is also required in website development...

    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.