导读:
?php ------------------------------------------------------------------------- 使用方法 测试时还需要一张表至少出现字段an...
简单的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);
-
-
-
-
-
mysql_query("SET NAMES 'UTF8'");
-
mysql_select_db($db_name,$conn) or die ('none DB');
-
-
-
$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');
-
-
$pages = new Pages;
-
$getPage = $pages->resultNUM($config);
-
-
-
class Pages{
-
public static $perNUM;
-
public static $touNUM;
-
public static $pageNUM;
-
public static $tables;
-
public static $errorURL;
-
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(){
-
if(isset($_GET[$this->numPage]) && !emptyempty($_GET[$this->numPage])){
-
$str = $_GET[$this->numPage];
-
}else{
-
$str = 'wrong';
- }
-
return $str;
- }
-
-
function getError(){
-
-
header('location: '.$this->errorURL);
- }
-
-
-
function isNUM(){
-
$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 = '
-
'.$singleResult.'… "moreInfoCon'.$announceID.'.html">详细
- ';
-
$allResult = $allResult.$singleResult;
- }
-
-
$mynum++;
-
-
if($mynum>$lastNUM){
-
$resultPage = '
class="pagination">
-
'.$beginPage.'
-
'.$lastPage.'
-
'.$getFivePage.'
-
'.$nextPage.'
-
'.$endPage.'
-
';
-
$allResult = $allResult.$resultPage;
-
return $allResult;
- }
-
- }
-
$resultPage = '
class="pagination">
-
'.$beginPage.'
-
'.$lastPage.'
-
'.$getFivePage.'
-
'.$nextPage.'
-
'.$endPage.'
-
';
-
$allResult = $allResult.'
'.$resultPage;
-
return $allResult;
- }
-
-
-
function nextPage(){
-
$nowPage = self::isNUM();
-
$pageNUM = $this->pageNUM;
-
if($nowPage>=$pageNUM){
-
$nextPage = '';
-
}else{
-
$nextPage = $nowPage+1;
-
$nextPage = '.$this->numPage.'='.$nextPage.'" title="Next Page">下一页 »';
- }
-
return $nextPage;
- }
-
-
-
function lastPage(){
-
$nowPage = self::isNUM();
-
if($nowPage>=2){
-
$lastPage = $nowPage-1;
-
$lastPage = '.$this->numPage.'='.$lastPage.'" title="Previous Page">« 上一页';
-
}else{
-
$lastPage = '';
- }
-
return $lastPage;
- }
-
-
-
function endPage(){
-
$pageNUM = $this->pageNUM;
-
$endPage = '.$this->numPage.'='.$pageNUM.'" title="Last Page">最后一页 »';
-
return $endPage;
- }
-
-
function beginPage(){
-
$beginPage = '.$this->numPage.'=1" title="First Page">« 第一页';
-
return $beginPage;
- }
-
-
function getFivePage(){
-
$nowPage = self::isNUM();
-
$pageNUM = $this->pageNUM;
-
if($pageNUM<=5){
-
$NUM = 1;
-
$getNUM = '';
-
while($pageNUM>=$NUM){
-
$nums = '.$this->numPage.'='.$NUM.'" class="number" title="'.$NUM.'">'.$NUM.'';
-
$getNUM = $getNUM.$nums;
-
$NUM++;
- }
-
$getFivePage = $getNUM;
-
return $getFivePage;
-
}else{
-
if($nowPage == 1){
-
$getNUM = '
-
"moreInfo.html?'.$this->numPage.'=1" class="number current" title="1">1
-
"moreInfo.html?'.$this->numPage.'=2" class="number" title="2">2
-
"moreInfo.html?'.$this->numPage.'=3" class="number" title="3">3
-
"moreInfo.html?'.$this->numPage.'=4" class="number" title="4">4
-
"moreInfo.html?'.$this->numPage.'=5" class="number" title="5">5
- ';
-
}elseif($nowPage == 2){
-
$getNUM = '
-
"moreInfo.html?'.$this->numPage.'=1" class="number" title="1">1
-
"moreInfo.html?'.$this->numPage.'=2" class="number current" title="2">2
-
"moreInfo.html?'.$this->numPage.'=3" class="number" title="3">3
-
"moreInfo.html?'.$this->numPage.'=4" class="number" title="4">4
-
"moreInfo.html?'.$this->numPage.'=5" class="number" title="5">5
- ';
-
}elseif($nowPage == ($pageNUM-1)){
-
$getNUM = '
-
"moreInfo.html?'.$this->numPage.'='.($pageNUM-4).'" class="number" title="'.($pageNUM-4).'">'.($pageNUM-4).'
-
"moreInfo.html?'.$this->numPage.'='.($pageNUM-3).'" class="number" title="'.($pageNUM-3).'">'.($pageNUM-3).'
-
"moreInfo.html?'.$this->numPage.'='.($pageNUM-2).'" class="number" title="'.($pageNUM-2).'">'.($pageNUM-2).'
-
"moreInfo.html?'.$this->numPage.'='.($pageNUM-1).'" class="number current" title="'.($pageNUM-1).'">'.($pageNUM-1).'
-
"moreInfo.html?'.$this->numPage.'='.$pageNUM.'" class="number" title="'.$pageNUM.'">'.$pageNUM.'
- ';
-
}elseif($nowPage == $pageNUM){
-
$getNUM = '
-
"moreInfo.html?'.$this->numPage.'='.($nowPage-4).'" class="number" title="'.($nowPage-4).'">'.($nowPage-4).'
-
"moreInfo.html?'.$this->numPage.'='.($nowPage-3).'" class="number" title="'.($nowPage-3).'">'.($nowPage-3).'
-
"moreInfo.html?'.$this->numPage.'='.($nowPage-2).'" class="number" title="'.($nowPage-2).'">'.($nowPage-2).'
-
"moreInfo.html?'.$this->numPage.'='.($nowPage-1).'" class="number" title="'.($nowPage-1).'">'.($nowPage-1).'
-
"moreInfo.html?'.$this->numPage.'='.$nowPage.'" class="number current" title="'.$nowPage.'">'.$nowPage.'
- ';
-
}elseif(2<$nowPage && $nowPage<($pageNUM-1)){
-
$getNUM = '
-
"moreInfo.html?'.$this->numPage.'='.($nowPage-2).'" class="number" title="'.($nowPage-2).'">'.($nowPage-2).'
-
"moreInfo.html?'.$this->numPage.'='.($nowPage-1).'" class="number" title="'.($nowPage-1).'">'.($nowPage-1).'
-
"moreInfo.html?'.$this->numPage.'='.$nowPage.'" class="number current" title="'.$nowPage.'">'.$nowPage.'
-
"moreInfo.html?'.$this->numPage.'='.($nowPage+1).'" class="number" title="'.($nowPage+1).'">'.($nowPage+1).'
-
"moreInfo.html?'.$this->numPage.'='.($nowPage+2).'" class="number" title="'.($nowPage+2).'">'.($nowPage+2).'
- ';
-
}else{
- self::getError();
- }
-
- }
-
$getFivePage = $getNUM;
-
return $getFivePage;
- }
-
-
- }
- ?>