一个简单的php分页逻辑

php分页

<?php
include 'backend/conn.php';

$html = '<ul>';     //输出的html
$pageDataNum=3;    //每页显示10行
$pageCount=0;   //页数 
$currentIndex=1;//当前第几页
$dataNum = 0;  //数据条数
$curentDataIndex = 0; //当前第几行数据
$pre = 0;             //上一页页数
$nex = 0;              //下一页页数
$preImageName = '';      //是否到页码开始的尽头
$nexImageName = '';      //是否到页码结尾的尽头

$page = isset($_REQUEST['page'])?$_REQUEST['page']:1;
$page = daddslashes($page);

$sql = "select count(1) as countNum from GsSpecialArticle";
$result = mysql_query($sql) or die(mysql_error());
if($row = mysql_fetch_object($result)){
    $dataNum=$row->countNum;
}
$pageCount = ceil($dataNum/$pageDataNum);
$curentDataIndex = ($page-1) * $pageDataNum;
$curentDataIndex = $curentDataIndex > $dataNum ? ($curentDataIndex-$pageDataNum) : $curentDataIndex;

$sql = "select * from GsSpecialArticle order by CreateTime desc limit ".$curentDataIndex.','.$pageDataNum.';';
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_object($result)){
    $html.='<li>'.substr($row->CreateTime,0,10).'</span>'.'<span class="title"><a href="01maincontent.html?id=14&subId=17&specialId='.$row->Id.'">'.$row->ArticleTitle.'</span></a></li>';
}

$pre = $page - 1;
$pre = $pre < 1 ? 1 : $pre;
$nex = $page + 1;
$nex = $nex > $pageCount ? $pageCount : $nex;


$html .= '</ul><ul id="pages">';

if($page == 1){
    $html .= '<li><span><image alt="" src="images/page_frist1.png"></image></span>
    <span><image alt="" src="images/page_prev1.png"></image></span></span>';
    for($i=1;$i<=$pageCount;$i++){
        if(abs($i-$page)<7){
            if($i == $page){
                $html .= '<span style="color:red;">'.$i.'</span>';
            }
            else{
                $html .= '<span><a href="#anchor" onclick="ChangePage('.$i.')">'.$i.'</image></a></span>';
            }
        }
    }
    $html .= '<span><a href="#anchor" onclick="ChangePage('.$nex.')"><image alt="" src="images/page_next.png"></image></a></span>
    <span><a href="#anchor" onclick="ChangePage('.$pageCount.')"><image alt="" src="images/page_last.png"></image></a></span></li>';
}
else if($page == $pageCount){
    //echo $page.'</br>';
    //echo $pageCount.'</br>';
    $html .= '<li><span><a href="#anchor" onclick="ChangePage(1)"><image alt="" src="images/page_frist.png"></image></a></span>
    <span><a href="#anchor" onclick="ChangePage('.$pre.')"><image alt="" src="images/page_prev'.$preImageName.'.png"></image></a></span>';
    for($i=1;$i<=$pageCount;$i++){
        if(abs($i-$page)<7){
            if($i == $page){
                $html .= '<span style="color:red;">'.$i.'</span>';
            }
            else{
                $html .= '<span><a href="#anchor" onclick="ChangePage('.$i.')">'.$i.'</image></a></span>';
            }
        }
    }    
    $html .= '<span><image alt="" src="images/page_next1.png"></image></span><span><image alt="" src="images/page_last1.png"></image></span></li>';
}
else{
    $html .= '<li><span><a href="#anchor" onclick="ChangePage(1)"><image alt="" src="images/page_frist.png"></image></a></span>
    <span><a href="#anchor" onclick="ChangePage('.$pre.')"><image alt="" src="images/page_prev.png"></image></a></span>';
    for($i=1; $i<=$pageCount ; $i++){
        if(abs($i-$page)<4){
            if($i == $page){
                $html .= '<span style="color:red;">'.$i.'</span>';
            }
            else{
                $html .= '<span><a href="#anchor" onclick="ChangePage('.$i.')">'.$i.'</image></a></span>';
            }
        }
    }
    $html .= '<span><a href="#anchor" onclick="ChangePage('.$nex.')"><image alt="" src="images/page_next.png"></image></a></span>
    <span><a href="#anchor" onclick="ChangePage('.$pageCount.')"><image alt="" src="images/page_last.png"></image></a></span></li>';
}

$html .= '</ul>';

echo $html;
?>
function ChangePage(page){
    window.location.href = "01maincontent.html?id=14&subId=17&page="+page;
}
原文地址:https://www.cnblogs.com/tylerdonet/p/3643996.html