-
简单的php分页类
- <?php
- //-------------------------------------------------------------------------
- //使用方法
- //测试时还需要一张表 至少出现字段 announceID,announceContent
- //include db.php
- $db_host='localhost';
- $db_user='username';
- $db_password='password';
- $db_name='konwu';
- $db_prefix = 'konwu_';
- Global $db_prefix;
- $conn=@mysql_connect($db_host,$db_user,$db_password);
- // utf-8 是默认设置 即使没有下面的一行也会默认 UTF-8
- // 但是MYSQL数据库的默认是LATIN 拉丁字符
- // 最好设置成 GBK UTF-8包含GBK 包含 LATIN
- //网页用UTF-8 显示 GBK 没关系 但如果 MYSQL写入时用UTF8有乱码风险,因为MYSQL安装时默认LATIN ,最好 SET GBK
- mysql_query("SET NAMES 'UTF8'");
- mysql_select_db($db_name,$conn) or die ('none DB');
- //set page
- //@param set
- $errorURL = 'http://localhost:8080/htmls/error/index.html';
- $result = mysql_query("SELECT * FROM konwu_messageannounce");
- $touNUM = mysql_num_rows($result);
- $config = array('DB_PREFIX'=>'konwu_','DB'=>'messageannounce','PER_NUM'=>10,'TOU_NUM'=>$touNUM,'ERROR_URL'=>$errorURL,'NUM_PAGE'=>'p');
- // use class
- $pages = new Pages;
- $getPage = $pages->resultNUM($config);//
- //------------------------------------
- class Pages{
- public static $perNUM;//每一页多少条
- public static $touNUM;//总共多少条
- public static $pageNUM;//总页数
- public static $tables;//表名
- public static $errorURL;// index URL
- public static $numPage;//页面参数的标识 ,
- function getInt($config=array()){
- $this->tables = $config['DB_PREFIX'].$config['DB'] ? $config['DB_PREFIX'].$config['DB'] : 'konwu_messageannounce';
- $this->perNUM = $config['PER_NUM'] ? $config['PER_NUM'] : 10;
- $this->touNUM = $config['TOU_NUM'] ? $config['TOU_NUM'] : 50;
- $this->pageNUM = ceil($this->touNUM/$this->perNUM);
- $this->errorURL = $config['ERROR_URL'] ? $config['ERROR_URL'] : 'http://www.konwu.com';
- $this->numPage = $config['NUM_PAGE'] ? $config['NUM_PAGE'] : 'page';
- }
- function getNUM(){//get num 得到当前页面的值
- if(isset($_GET[$this->numPage]) && !emptyempty($_GET[$this->numPage])){
- $str = $_GET[$this->numPage];
- }else{
- $str = 'wrong';
- }
- return $str;
- }
- function getError(){
- //return $str;
- header('location: '.$this->errorURL);
- }
- //$_GET['pageNUM'] 第几页,必须要是数字整数,必须要 >0 ,必须 <=$pageNUM
- function isNUM(){//check num
- $str = self::getNUM();
- if($str != 'wrong'){
- if(ctype_digit($str)){
- $numPage = $str;
- }else{
- self::getError();
- }
- }else{
- self::getError();
- }
- return $numPage;
- }
- function getSelect(){
- $sql = 'SELECT * FROM '.$this->tables.' ORDER BY announceID DESC';
- return $sql;
- }
- function getSelectResult(){
- $sql = self::getSelect();
- $result = mysql_query($sql);
- return $result;
- }
- function getStartNUM(){
- $nowPage = self::isNUM();
- $perNUM = $this->perNUM;
- $startNUM = $nowPage-1;
- $startNUM = $startNUM*$perNUM;
- $startNUM = $startNUM+1;
- return $startNUM;
- }
- function getLastNUM(){
- $nowPage = self::isNUM();
- $perNUM = $this->perNUM;
- $lastNUM = $nowPage*$perNUM;
- return $lastNUM;
- }
- function resultNUM($config){
- self::getInt($config);
- $result = self::getSelectResult();
- $startNUM = self::getStartNUM();
- $lastNUM = self::getLastNUM();
- $mynum = 1;
- $allResult = '';
- while($re=mysql_fetch_array($result)){
- if($mynum>=$startNUM && $mynum<=$lastNUM){
- $announceID = $re['announceID'];
- $singleResult = $re['announceContent'];
- $singleResult = mb_substr($singleResult,0,50,'utf-8');
- $lastPage = self::lastPage();
- $nextPage = self::nextPage();
- $beginPage = self::beginPage();
- $endPage = self::endPage();
- $getFivePage = self::getFivePage();
- $singleResult = '
- <p>'.$singleResult.'… <a href="moreInfoCon'.$announceID.'.html">详细</a></p>
- ';
- $allResult = $allResult.$singleResult;
- }
- //--------------------------------------------------------------
- $mynum++;
- //----------------------------
- if($mynum>$lastNUM){
- $resultPage = '<div class="pagination">
- '.$beginPage.'
- '.$lastPage.'
- '.$getFivePage.'
- '.$nextPage.'
- '.$endPage.'
- </div>';
- $allResult = $allResult.$resultPage;
- return $allResult;
- }
- //-----------------------------
- }
- $resultPage = '<div class="pagination">
- '.$beginPage.'
- '.$lastPage.'
- '.$getFivePage.'
- '.$nextPage.'
- '.$endPage.'
- </div>';
- $allResult = $allResult.'<br><br>'.$resultPage;
- return $allResult;
- }
- //next page下一页
- function nextPage(){
- $nowPage = self::isNUM();
- $pageNUM = $this->pageNUM;
- if($nowPage>=$pageNUM){
- $nextPage = '';
- }else{
- $nextPage = $nowPage+1;
- $nextPage = '<a href="moreInfo.html?'.$this->numPage.'='.$nextPage.'" title="Next Page">下一页 »</a>';
- }
- return $nextPage;//得到参数值
- }
- //lastPage 上一页
- function lastPage(){
- $nowPage = self::isNUM();
- if($nowPage>=2){
- $lastPage = $nowPage-1;
- $lastPage = '<a href="moreInfo.html?'.$this->numPage.'='.$lastPage.'" title="Previous Page">« 上一页</a>';
- }else{
- $lastPage = '';
- }
- return $lastPage;
- }
- //第一页,最后一页,当前页左右各两页共五页
- function endPage(){
- $pageNUM = $this->pageNUM;
- $endPage = '<a href="moreInfo.html?'.$this->numPage.'='.$pageNUM.'" title="Last Page">最后一页 »</a>';
- return $endPage;
- }
- function beginPage(){
- $beginPage = '<a href="moreInfo.html?'.$this->numPage.'=1" title="First Page">« 第一页</a>';
- return $beginPage;
- }
- function getFivePage(){
- $nowPage = self::isNUM();//当前页面
- $pageNUM = $this->pageNUM;//总页数
- if($pageNUM<=5){
- $NUM = 1;
- $getNUM = '';
- while($pageNUM>=$NUM){
- $nums = '<a href="moreInfo.html?'.$this->numPage.'='.$NUM.'" class="number" title="'.$NUM.'">'.$NUM.'</a>';
- $getNUM = $getNUM.$nums;
- $NUM++;
- }
- $getFivePage = $getNUM;
- return $getFivePage;
- }else{//>5
- if($nowPage == 1){
- $getNUM = '
- <a href="moreInfo.html?'.$this->numPage.'=1" class="number current" title="1">1</a>
- <a href="moreInfo.html?'.$this->numPage.'=2" class="number" title="2">2</a>
- <a href="moreInfo.html?'.$this->numPage.'=3" class="number" title="3">3</a>
- <a href="moreInfo.html?'.$this->numPage.'=4" class="number" title="4">4</a>
- <a href="moreInfo.html?'.$this->numPage.'=5" class="number" title="5">5</a>
- ';
- }elseif($nowPage == 2){
- $getNUM = '
- <a href="moreInfo.html?'.$this->numPage.'=1" class="number" title="1">1</a>
- <a href="moreInfo.html?'.$this->numPage.'=2" class="number current" title="2">2</a>
- <a href="moreInfo.html?'.$this->numPage.'=3" class="number" title="3">3</a>
- <a href="moreInfo.html?'.$this->numPage.'=4" class="number" title="4">4</a>
- <a href="moreInfo.html?'.$this->numPage.'=5" class="number" title="5">5</a>
- ';
- }elseif($nowPage == ($pageNUM-1)){//-2位置
- $getNUM = '
- <a href="moreInfo.html?'.$this->numPage.'='.($pageNUM-4).'" class="number" title="'.($pageNUM-4).'">'.($pageNUM-4).'</a>
- <a href="moreInfo.html?'.$this->numPage.'='.($pageNUM-3).'" class="number" title="'.($pageNUM-3).'">'.($pageNUM-3).'</a>
- <a href="moreInfo.html?'.$this->numPage.'='.($pageNUM-2).'" class="number" title="'.($pageNUM-2).'">'.($pageNUM-2).'</a>
- <a href="moreInfo.html?'.$this->numPage.'='.($pageNUM-1).'" class="number current" title="'.($pageNUM-1).'">'.($pageNUM-1).'</a>
- <a href="moreInfo.html?'.$this->numPage.'='.$pageNUM.'" class="number" title="'.$pageNUM.'">'.$pageNUM.'</a>
- ';
- }elseif($nowPage == $pageNUM){//-1位置
- $getNUM = '
- <a href="moreInfo.html?'.$this->numPage.'='.($nowPage-4).'" class="number" title="'.($nowPage-4).'">'.($nowPage-4).'</a>
- <a href="moreInfo.html?'.$this->numPage.'='.($nowPage-3).'" class="number" title="'.($nowPage-3).'">'.($nowPage-3).'</a>
- <a href="moreInfo.html?'.$this->numPage.'='.($nowPage-2).'" class="number" title="'.($nowPage-2).'">'.($nowPage-2).'</a>
- <a href="moreInfo.html?'.$this->numPage.'='.($nowPage-1).'" class="number" title="'.($nowPage-1).'">'.($nowPage-1).'</a>
- <a href="moreInfo.html?'.$this->numPage.'='.$nowPage.'" class="number current" title="'.$nowPage.'">'.$nowPage.'</a>
- ';
- }elseif(2<$nowPage && $nowPage<($pageNUM-1)){//2位置和-2位置之间
- $getNUM = '
- <a href="moreInfo.html?'.$this->numPage.'='.($nowPage-2).'" class="number" title="'.($nowPage-2).'">'.($nowPage-2).'</a>
- <a href="moreInfo.html?'.$this->numPage.'='.($nowPage-1).'" class="number" title="'.($nowPage-1).'">'.($nowPage-1).'</a>
- <a href="moreInfo.html?'.$this->numPage.'='.$nowPage.'" class="number current" title="'.$nowPage.'">'.$nowPage.'</a>
- <a href="moreInfo.html?'.$this->numPage.'='.($nowPage+1).'" class="number" title="'.($nowPage+1).'">'.($nowPage+1).'</a>
- <a href="moreInfo.html?'.$this->numPage.'='.($nowPage+2).'" class="number" title="'.($nowPage+2).'">'.($nowPage+2).'</a>
- ';
- }else{
- self::getError();
- }
- }
- $getFivePage = $getNUM;
- return $getFivePage;
- }
- //----------------------------------
- }
- ?>
出处:http://www.phpfensi.com/php/20131114/184.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式