PHP paging A simple introduction to the simplest PHP paging code implementation, pagination _php tutorial

Source: Internet
Author: User

PHP pagination on a simple implementation of PHP paging code, paging


PHP paging code in a variety of program development is necessary to use, in the development of the Web site is a necessary choice.

To write the paging code, first you have to understand the SQL query statement: SELECT * from goods limit 2, 7. PHP Paging code core is to expand around this statement, SQL statement Description: Query goods data table from the 2nd data start to remove 7 data. In the pagination code, 7 indicates how many items are displayed per page, 2 uses the formula to calculate the number of page turns, and by passing in different parameters to replace the value "2", you can filter out different data.

index.php:

Include ' conn.php ';  Introduction of database Operation Class $conn=new Conn (); Instantiate the database operation class $total= $conn->getone (' Select COUNT (*) as total from goods '); $total = $total [' Totals '];//goods table data The overall number of data bars num=6;//shows the number of bars per page $totalpage=ceil ($total/$num);//Count pages if (isset ($_get[' page ') && $_get[' page ']<= $totalpage {//Here makes a judgment, if get to the data and the data is less than the total number of pages to pay the current page parameters, otherwise jump to the first page $thispage=$_get[' page '];} else{$thispage = 1;}
Note the red part of the SQL statement below, calculated to determine starting from the first few data, the current page minus 1 multiplied by the number of display data 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 ']. '
  • ';}
    Displays a numbered list of pagination for ($i =1; $i <= $totalpage; $i + +) {echo '. $i. ';}

    The code above implements one of the simplest PHP paging effects:

    Only realize the click-to-turn digital display different paging data, can be further improved on this basis, as long as the basic principles of understanding, follow-up work is relatively easy to develop.

    conn.php Code:

    /** connection 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 ', ' 123456 '); $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 ();}} Gets a row of data public function GetOne ($sql) {$rs = $this->pdo->query ($sql)->fetch (PDO::FETCH_ASSOC); return $rs;} Get Multiline data result public function GetAll ($sql) {$rs = $this->pdo->query ($sql)->fetchall (PDO::FETCH_ASSOC); return $ RS;}}

    conn.php function is to complete the database connection, and implementation of data extraction operation method, here I use PDO, here can be used to organize code according to people.

    config.php:

    /** Configuration Database Information */$cfg _dbhost= ' localhost '; $cfg _dbname= ' test '; $cfg _dbuser= ' root '; $cfg _dbpw= ' 123456 ';

      

    This example is only to illustrate the underlying paging principle, and there are many modifications to the actual use.

    Sample code Download: http://www.superphp.cn/yuanma/332.html

    http://www.bkjia.com/PHPjc/1135470.html www.bkjia.com true http://www.bkjia.com/PHPjc/1135470.html techarticle PHP Paging A simple introduction to the simplest implementation of PHP paging code, pagination discussion PHP page code in a variety of program development is necessary to use, in the development of the Web site is a necessary choice. Want to ...

  • 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.