php分页程序paginator程序代码

#*********************************************************#文件名称:function.php#功能描述:新闻添加修改处理模块#程序制作:留印(adleyliu)#联系qq:14339095#联系邮箱:[email=adleyliu@163.com]adleyliu@163.com[/email]#官方网站:[url=][/url]#copyright(c)2007-2008115000.com.cnallrightsreserved.#最后更新:2007-11-20#*********************************************************#*********************************************************#分页函数#*********************************************************functionyl_list_page($pageurl,$rsnum,$pages,$pagecount,$pagesize){#url#总记录#总页数#当前页码#每页显示数//$pageurl=’?’;$pcount=$pages;$page_info=’divclass=pagenum’;$page_info.=’divclass=num’;if(($pcount1)($pcount==$pagecount)){$page_info.=’ahref=’.$pageurl.’page=’.intval($pagecount-1).’/a’;}elseif(($pagecount!=1)($pcount!=$pagecount)){$page_info.=’ahref=’.$pageurl.’page=’.intval($pagecount-1).’/a’;}$page_info.=’ahref=’.$pageurl.’page=1页首/a’;if($pagecount4){$page_info.=’ahref=’.$pageurl.’page=1[1]/aspanclass=dot…/span’;}if($pcount$pagecount+2){$endpage=$pagecount+2;}else{$endpage=$pcount;}for($n=($pagecount-2);$n=$endpage;$n++){if(!($n1)){if($n==intval($pagecount)){$page_info.=’spanclass=normal’.$n.’/span’;}else{$page_info.=’ahref=’.$pageurl.’page=’.$n.'[‘.$n.’]/a’;}}}if($pagecount+2$pcount){$page_info.=’spanclass=dot…/spanahref=’.$pageurl.’page=’.$pcount.'[‘.$pcount.’]/a’;}$page_info.=’ahref=’.$pageurl.’page=’.$pcount.’页尾/a’;if(($pagecount==1)($pcount!=$pagecount)($pcount!=0)){$page_info.=’ahref=’.$pageurl.’page=’.intval($pagecount+1).’/a’;}elseif(($pagecount!=1)($pcount!=$pagecount)){$page_info.=’ahref=’.$pageurl.’page=’.intval($pagecount+1).’/a’;}$page_info.=’/div/div’;$page_info.=’divclass=pagenum’;$page_info.=’divclass=numspanclass=normal共:’.$rsnum.’条/’.$pcount.’页每页/’.$pagesize.’条/span/div’;$page_info.=’divclass=num’;//echo’formname=pageaction=’.$pageurl.”;$page_info.=’转到’;$page_info.=’inputtype=textname=pagevalue=/’1/’class=login_leftstyle=/’width:28px;height:18px;/”;$page_info.=’页inputtype=submitname=submit3class=login_submitstyle=/’width:28px;height:18px;padding-top:1px;/’onclick=document.myform.action.value=/’go/”;//echo’/form’;$page_info.=’/div’;$page_info.=’/div’;return$page_info;}

 代码如下

 代码如下

<?php
 
class paginator {
 
    function __construct () {
 
    }
 
    static function render($page_type, array $args = array()) {
        $html = ”;
        switch ($page_type)
        {
            case ‘1’:
                $html =  page_1($channel_code, $page_num, $cur_page,
$per_group, $base_url, $n, $v);
                break;
            case ‘2’:
                $html =  fpage($row_num, $pagesize, $page, $url);
                break;
            case ‘3’:
                break;
 
            default:
                $html = page($num, $perpage, $curpage, $mpurl,
$hiddenFrom=”);
        }
 
        return $html;
    }
 
 
 
   
//函数1:根据总页数,当前页,和页分组及url产生分页导航
    function page_1($channel_code, $page_num, $cur_page,
$per_group, $base_url, $n, $v) {
        // 分页函数参数列表(有多少页,当前页,每页多少个 ,链接地址)
        $base_url .= strpos($base_url, ‘?’) ? ‘&’ : ‘?’;   
//如果有?,则添加&,否则加?
 
        $page_val = “”;
 
        if ($page_num < 1) {
            return $page_val; //如果小于1,返回空
        } elseif ($page_num == 1) {
            return $page_val = 1;   //如果等于1,返回1
        }
 
        //分组
 
        $group_ok = !(($page_num-1) % ($per_group-1));  //没有余数
        // 计算一共有多少组
        if($group_ok) {
            $group_num = ($page_num-1)/($per_group-1);
        }else {
            $group_num = ceil(($page_num-1) / ($per_group-1));
        } //判断当前页是否为一组的最后一个
 
 
        //组数
        $is_last_page = !(($cur_page-1) % ($per_group-1));
//计算当前在的组数
        if ($is_last_page) {
            $cur_group = ($cur_page-1) / ($per_group-1) + 1;
        } else {
            $cur_group = ceil(($cur_page-1) / ($per_group-1));
        }
 
        // 计算显示的起始页 ,这个关键
        $from = ($cur_group – 1) * ($per_group-1) + 1;
        $from = $cur_group <= 1 ? 1 :$from;
 
        $to = $from + $per_group;
        $to = $to > $page_num ? $page_num + 1 : $to;
 
        for($a = $from; $a < $to; $a++) {
            if ($a != $cur_page) {
 
                //不是当前页
                $page_val .= ‘<li><a href =”‘ . $base_url .
‘channel=’.$channel_code.’&n=’.$n.’&v=’.$v.’&page=’ . $a . ‘”>’ . $a
. ‘</a></li>’;
            } else {
                //当前页
                $page_val .='<li class=”current”>’. $a
.'</li>’;
            }
        }
 
        //上一页下一页首页尾页
        $next = $cur_page + 1;    //当前页+1
        $prev = $cur_page-1;   //当前页-1
 
        if ($cur_page != $page_num) {
            //当前页不是最后一页
            $page_val .= ‘<span><a href =”‘ . $base_url .
‘channel=’.$channel_code.’&n=’.$n.’&v=’.$v.’&page=’ . $next . ‘”>’ .
‘下一页’ . ‘</a></span>’;
            $page_val .= ‘<span><a href =”‘ . $base_url .
‘channel=’.$channel_code.’&n=’.$n.’&v=’.$v.’&page=’ . $page_num .
‘”>’ . ‘>>’ . ‘</a></span>’; //尾页
 
        }
        if ($cur_page != 1){
 
            //不是第一页,加上前面的内容
            $page_val = ‘<span><a href =”‘ . $base_url .
‘channel=’.$channel_code.’&n=’.$n.’&v=’.$v.’&page=’ . $prev . ‘”>’ .
‘上一页’ . ‘</a></span>’ . $page_val;
            $page_val = ‘<span><a href =”‘ . $base_url .
‘channel=’.$channel_code.’&n=’.$n.’&v=’.$v.’&page=1″>’ . ‘<<‘
. ‘</a></span>’ . $page_val; //首页
 
        }
        $page_val = ‘<ul
class=”page_nav”>’.$page_val.'</ul>’;
 
 
        return $page_val;
    }
 
 
 
 
    // 函数2:根据记录数,页列清数,当前页,url年成分页
    function fpage($row_num, $pagesize, $page, $url) {
       
//$page,当前页;$row_num记录总数;$pagesize:每页记录数;$url记录页;
        //如果,如果有作数,则取加1
        $page_count = $row_num % $pagesize?intval($row_num /
$pagesize) + 1:intval($row_num / $pagesize);
 
        if ($page_count == 1) {
            $page_string = “第一页|上一页|下一页|尾页”;
        } else {
            //如果不止一页
            if ($page == 1) {
                //第一页显示
                $page_string = ” <a href=” . $url .
“?page=1>第一页 </a>|上一页| <a href=”%20.%20$url%20.%20″?page=” .
($page + 1) . “>下一页 </a>| <a href=”%20.%20$url%20.%20″?page=” .
$page_count . “>尾页 </a>”;
            }
            if (($page > 1) and ($page < $page_count)) {
                $page_string = ” <a href=” . $url .
“?page=1>第一页 </a>| <a href=”%20.%20$url%20.%20″?page=” .
($page-1) . “>上一页 </a>| <a href=”%20.%20$url%20.%20″?page=” .
                ($page + 1) . “>下一页 </a>| <a href=” .
$url . “?page=” . $page_count . “>尾页 </a>”;
            }
            if ($page == $page_count) {
                $page_string = ” <a href=” . $url .
“?page=1>第一页 </a>| <a href=”%20.%20$url%20.%20″?page=” .
($page-1) . “>上一页 </a>|下一页| <a href=” . $url .
“?page=” . $page_count . “>尾页 </a>”;
            }
        }
        $page_str = $page_string . (“,” . $page . “/” . $page_count .
“页”);
        echo ” <br> <p align=center>” . $page_str . ”
</p>”;
    }
 
 
    /**
 * 分页函数
 *
 * @param int $num:记录总数
 * @param int $perpage:每页记录
 * @param int $curpage:当前页
 * @param string $mpurl:路径url
 * @param unknown_type $hiddenFrom
 * @return string
 */
    function page($num, $perpage, $curpage, $mpurl,$hiddenFrom=”) {
        $multipage = ”;
        //恢复
        $mpurl .= strpos($mpurl, ‘?’) ? ‘&’ : ‘?’;
 
        if($num > $perpage) {
            $page = 10;
            $offset = 2;
 
            $pages = @ceil($num / $perpage);  //总页数
 
            if($page > $pages) {
                $from = 1;
                $to = $pages;
            } else {
                //如果总页数>10
                $from = $curpage – $offset;   //当前页-2
                $to = $from + $page – 1;    //当前页+10-3
 
                if($from < 1) {
 
                    $to = $curpage + 1 – $from;
                    $from = 1;
                    if($to – $from < $page) {
                        $to = $page;
                    }
                } elseif($to > $pages) {
                    $from = $pages – $page + 1;
                    $to = $pages;
                }
            }
 
            if ($hiddenFrom) {
                $multipage = ($curpage – $offset > 1 && $pages >
$page ? ‘<a href=”#” ‘.js_page($hiddenFrom,1).’>首页</a> ‘
: ”).
                ($curpage > 1 ? ‘ <a href=”#”
‘.js_page($hiddenFrom,$curpage – 1).’>‹‹上一页</a> ‘ : ”);
                for($i = $from; $i <= $to; $i++) {
                    $multipage .= $i == $curpage ?
‘<span>’.$i.'</span> ‘ :
                    ‘<a href=”#”
‘.js_page($hiddenFrom,$i).’>’.$i.'</a> ‘;
                }
 
                $multipage .= ($curpage < $pages ? ‘<a href=”#”
‘.js_page($hiddenFrom,$curpage + 1).’>下一页››</a>’ : ”).
                ($to < $pages ? ‘<a href=”#”
‘.js_page($hiddenFrom,$pages).’> 末页</a>’ : ”);
                $multipage = $multipage ? ‘<div
class=”list-page”>’.$multipage.'</div>’ : ”;
            } else {
 
                $multipage = ($curpage – $offset > 1 && $pages >
$page ? ‘<a href=”‘.$mpurl.’page=1″>首页</a> ‘ : ”).
                ($curpage > 1 ? ‘ <a
href=”‘.$mpurl.’page=’.($curpage%20-%201).'”>‹‹上一页</a> ‘ :
”);
 
                for($i = $from; $i <= $to; $i++) {
                    $multipage .= $i == $curpage ?
‘<span>’.$i.'</span> ‘ :
                    ‘<a
href=”‘.$mpurl.’page=’.$i.'”>’.$i.'</a> ‘;
                }
 
                $multipage .= ($curpage < $pages ? ‘<a
href=”‘.$mpurl.’page=’.($curpage%20+%201).'”>下一页››</a>’ : ”).
                ($to < $pages ? ‘<a
href=”‘.$mpurl.’page=’.$pages.'”> 末页</a>’ : ”);
                $multipage = $multipage ? ‘<div
class=”list-page”>’.$multipage.'</div>’ : ”;
            }
        }
        return $multipage;
    }
    //暂不用
    function page_get_start($page, $ppp, $totalnum) {
        $totalpage = ceil($totalnum / $ppp);
        $page =  max(1, min($totalpage, intval($page)));
        return ($page – 1) * $ppp;
    }
    //暂不用
    function js_page($hiddenFrom,$page)
    {
        if ($hiddenFrom) {
            return ‘
onclick=javascript:$(“#page”).val(“‘.$page.'”);$(“#’.$hiddenFrom.'”).submit();
‘;
        }
    }
}
 
 
 
?>

$conn=mysql_connect($mysql_host,$mysql_user,$mysql_password) or
die(‘连接服务器出错’);
mysql_select_db($mysql_db) or
die(“选择数据库教程出错”);
mysql_query(“set names ‘gbk'”);
function table($t)
{
 global $mysql_table_prefix;
 return $mysql_table_prefix.$t;
}

$sql=”select * from “.table(‘keywords’).”  where 1=1 “;
$sql2=”select count(*) from “.table(‘keywords’).”  where 1=1 “;
$py=trim($_request[‘py’]);
if($act==”search” and !empty($_post[‘s1’]))
{
 $s1=trim($_post[‘s1’]);//获取搜索字
 $sql .=” and (k1=’$s1′ or k2=’$s1′) “;
 $sql2 .=” and (k1=’$s1′ or k2=’$s1′) “;
}
elseif(!empty($py))
{
 $sql .=” and pinyin=’$py’ “;
 $sql2.=” and pinyin=’$py’ “;
}

$sql .=” order by pinyin “;
$rscount=mysql_query($sql2);
$rscount=@mysql_fetch_array($rscount);
$rscount=$rscount[0];//获取总记录数

$page=trim($_get[‘page’]);//当前页数
$pagesize=30;//每页记录数
$pagecount=ceil($rscount/$pagesize);//获取总页数
$pageurl=””;
if($page<=1)
{
 $page=1;
 $pageurl.=” 首页 上一页”;
}else
{
 $pageurl .=” <a href=’?page=1&py=”.$py.”‘>首页</a>
 <a
href=’?py=”.$py.”&page=”.($page-1).”‘>上一页</a>”;
}
if($page>=$pagecount)
{
 $page=$pagecount;
 $pageurl .=” 下一页  尾页”;
}else
{
 $pageurl .=” <a
href=’?py=”.$py.”&page=”.($page+1).”‘>下一页</a>  <a
href=’?py=”.$py.”&page=”.$pagecount.”‘>尾页</a>”;
}

$start_rs=($page-1)*$pagesize;//开始
$end_rs=$page*$pagesize;//结束
$sql .=” limit {$start_rs},{$end_rs} “;
$res=mysql_query($sql);

?>

发表评论

电子邮件地址不会被公开。 必填项已用*标注