PHP paging a simple implementation of the simplest PHP page 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 ';//Introducing Database Operations Classes $conn=NewConn ();//instantiating a database operation class $total=$conn->getone (' Select COUNT (*) as total from goods ');$total=$total[' Total '];//Goods table Data total data Bar number$num= 6;//show number of bars per page$totalpage=Ceil($total/$num);//Count Pagesif(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{    $thispage=1;}<br>//note the red part of the SQL statement below, calculated to determine from the first few data to take out, the current page minus 1 multiplied by the number of data bars per page$sql= ' Select Goods_id,goods_name,shop_price from Goods order by goods_id limit '. <span style= "color: #ff0000;" > ($thispage-1) *$num</span>. ', '.$num.‘‘; $data=$conn->getall ($sql); foreach($data  as $k=$v){    Echo' <li> '.$v[' goods_id ']. ', '.$v[' Goods_name ']. ' ---¥ '.$v[' Shop_price ']. ' </li> '; }<br>//display a numbered list of pages for($i= 1;$i<=$totalpage;$i++){    Echo' <a href= '? page= '.$i.‘" > '.$i.‘ </a> '; }

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 database for related query operation*/ classconn{ Public function__construct () {include_once(' config.php '); Try{               $this->pdo =NewPDO (' 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(); }    }         //get a row of data     Public functionGetOne ($sql){        $rs=$this->pdo->query ($sql)->fetch (PDO::FETCH_ASSOC); return $rs; }         //get multiple rows of data results     Public functionGetAll ($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 ';

PHP paging a simple implementation of the simplest PHP page code

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.