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:

?
1234567891011121314151617181920212223242526 include ‘conn.php‘;  //引入数据库操作类  $conn=new conn();  //实例化数据库操作类  $total=$conn->getOne(‘select count(*) as total from goods‘); $total=$total[‘total‘]; //goods表数据总数据条数 $num=6; //每页显示条数 $totalpage=ceil($total/$num);  //计算页数 if(isset($_GET[‘page‘]) && $_GET[‘page‘]<=$totalpage){//这里做了一个判断,若get到数据并且该数据小于总页数情况下才付给当前页参数,否则跳转到第一页   $thispage=$_GET[‘page‘]; }else{   $thispage=1; } <BR>//注意下面sql语句中红色部分,通过计算来确定从第几条数据开始取出,当前页数减去1后再乘以每页显示数据条数 $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>//显示分页数字列表 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:

?
1234567891011121314151617181920212223242526272829303132333435 /* *连接数据库 进行相关查询操作 */  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 ‘数据库连接失败:‘.$e->getMessage();       exit();     }   }      //获取一行数据   public function getOne($sql){     $rs=$this->pdo->query($sql)->fetch(PDO::FETCH_ASSOC);          return $rs;   }      //获取多行数据结果   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:

?
12345678 * *配置数据库信息 */ $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.

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 ~

PHP paging a simple implementation of the simplest PHP paging 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.