PHP paging a simple implementation of the simplest PHP paging code

Source: Internet
Author: User
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 ';  Introduce database operation class  $c Conn (); Instantiate the database operation class  $total = $conn->getone (' Select COUNT (*) as total from goods '), $total = $total [' Total ']; Goods table data total Data Bar number $num = 6; Shows the number of bars per page $totalpage =ceil ($total/$num);  Calculate the number of pages if (Isset ($_get[' page ')) && $_get[' page ']<= $totalpage) {//Here is 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{   
Note the red part of the SQL statement below, calculated to determine 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:

    /* * Connect to database for related query operation */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;   }      Gets the 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:

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

    The above is a small part of the PHP page for everyone to explore a simple PHP page code of the simple implementation of all the content, I hope you like Oh ~

    If you want to know more game activities and the introduction of the game, please continue to pay attention to this site, the small series will be the first time for everyone to bring the best-looking, most fun, the freshest game information. More exciting content in the JB51 game channel!

    The above introduces the PHP paging to a simple implementation of the simplest PHP paging code, including the content, I hope to be interested in PHP tutorial friends helpful.

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