1)先将 html 包成函数,放到核心函数库
/**
* _paging 分页函数
* @param int $_type 1/2
* 1---数字分页
* 2---文本分页
*/
function _paging($_type){
//在这里拿不到值,做成全局变量就能拿到值了
global $_page,$_pageabsolute,$_num,$_id;
if($_type == 1){
//以数字分组
echo '<div id="page_num">';
echo '<ul>';
for($i=0;$i<$_pageabsolute;$i++){
if($_page == $i+1){
echo '<li><a href="'.SCRIPT.'.php?'.$_id.'page='.($i+1).'" class="selection">'.($i+1).'</a></li>';
}else{
echo '<li><a href="'.SCRIPT.'.php?'.$_id.'page='.($i+1).'">'.($i+1).'</a></li>';
}
}
echo '</ul>';
echo '</div>';
}elseif($_type == 2){
//以文本分组
echo '<div id="page_text">';
echo '<ul>';
echo '<li>'.$_page.'/'.$_pageabsolute.'页 | </li>';
echo '<li>共有<strong>'.$_num.'</strong>条数据 | </li>';
if($_page == 1){
echo '<li>首页 | </li>';
echo '<li>上一页 | </li>';
}else{
echo '<li><a href="'.SCRIPT.'.php">首页</a> | </li>';
echo '<li><a href="'.SCRIPT.'.php?'.$_id.'page='.($_page -1).'">上一页</a> | </li>';
}
if($_page == $_pageabsolute){
echo "<li>下一页 | </li>";
echo "<li>尾页 | </li>";
}else{
echo '<li><a href="'.SCRIPT.'.php?'.$_id.'page='.($_page +1).'">下一页</a> | </li>';
echo '<li><a href="'.SCRIPT.'.php?'.$_id.'page='.$_pageabsolute.'">尾页</a> </li>';
}
echo '</ul>';
echo '</div>';
}else{
//以数字分组
echo '<div id="page_num">';
echo '<ul>';
for($i=0;$i<$_pageabsolute;$i++){
if($_page == $i+1){
echo '<li><a href="'.SCRIPT.'.php?page='.($i+1).'" class="selection">'.($i+1).'</a></li>';
}else{
echo '<li><a href="'.SCRIPT.'.php?page='.($i+1).'">'.($i+1).'</a></li>';
}
}
echo '</ul>';
echo '</div>';
}
}
2)调用此函数
3)分页模块
function _page($_sql,$_size){
global $_pagesize,$_pagenum,$_page,$_pageabsolute,$_num;
//分页模块
if(isset($_GET['page'])){
$_page = $_GET['page'];
if(empty($_page) || $_page<=0 || !is_numeric($_page)){
$_page = 1;
}else{
$_page = intval($_page);
}
}else{
$_page = 1;
}
//一页12个
$_pagesize = $_size;
//首先要得到所有数据的总和--_num_rows--mysql.func.php中封装的函数
$_num = _num_rows(_query($_sql));
if($_num == 0){
$_pageabsolute = 1;
}else{
$_pageabsolute = ceil($_num / $_pagesize);
}
if($_page > $_pageabsolute){
$_page = $_pageabsolute;
}
$_pagenum = ($_page - 1) * $_pagesize;
//进一取整法 ceil
}
4)调用